php 操作 MySQL

if(!defined("WEB_ROOT")){
  exit("Access Deny !");
}

class DB_MySQL  {

	function DB_MySQL($servername, $dbusername, $dbpassword, $dbname, $usepconnect){
	  $this->server = $servername; 
	  $this->dbuser = $dbusername;
	  $this->dbpass = $dbpassword; 
	  $this->dbname = $dbname;
	  $this->pconn = $usepconnect;
	}

	var $querycount = 0;

	function geterrdesc() {
		return mysql_error();
	}

	function geterrno() {
		return intval(mysql_errno());
	}

	function insert_id() {
		$id = mysql_insert_id();
		return $id;
	}

	function connect() {
		if($this->pconn==1) {
			if(!@mysql_pconnect($this->server, $this->dbuser, $this->dbpass)) {
				$this->halt('数据库链接失败');
			}
		} else {
			if(!@mysql_connect($this->server, $this->dbuser, $this->dbpass)) {
				$this->halt('数据库链接失败');
			}
		}
		
		mysql_query("set names 'GBK'");
		
		if($this->dbname) {
			$this->select_db($this->dbname);
		}
	}

	function fetch_array($query, $result_type = MYSQL_ASSOC) {
		return mysql_fetch_array($query, $result_type);
	}

	function query($sql, $type = '') {
		//echo "<div style=\"text-align: left;\">".htmlspecialchars($sql)."</div>";
		/*
		遇到问题时用这个来检查SQL执行语句
		$fp = fopen('sqlquerylog.txt', 'a');
		flock($fp, 2);
		fwrite($fp, $sql."\n");
		fclose($fp);
		*/
		$func = $type == 'UNBUFFERED' && @function_exists('mysql_unbuffered_query') ?
			'mysql_unbuffered_query' : 'mysql_query';
		if(!($query = $func($sql)) && $type != 'SILENT') {
			$this->halt('MySQL Query Error', $sql);
		}
		$this->querycount++;
		return $query;
	}
	
	function unbuffered_query($sql) {
		$query = $this->query($sql, 'UNBUFFERED');
		return $query;
	}

	function select_db($dbname) {
		return mysql_select_db($dbname);
	}

	function fetch_row($query) {
		$query = mysql_fetch_row($query);
		return $query;
	}

	function fetch_one_array($query) {
		$result = $this->query($query);
		$record = $this->fetch_array($result);
		return $record;
	}

	function num_rows($query) {
		$query = mysql_num_rows($query);
		return $query;
	}

	function num_fields($query) {
		return mysql_num_fields($query);
	}
	
	function result($query, $row) {
		$query = @mysql_result($query, $row);
		return $query;
	}
	
	function free_result($query) {
		$query = mysql_free_result($query);
		return $query;
	}

	function version() {
		return mysql_get_server_info();
	}

	function close() {
		return mysql_close();
	}

	function halt($msg, $sql=''){
		global $php_self,$timestamp,$onlineip;

		if ($sql) {
			@$fp = fopen(SABLOG_ROOT.'log/dberrorlog.php', 'a');
			@fwrite($fp, "<?PHP exit('Access Denied'); ?>\t$timestamp\t$onlineip\t".basename($php_self)."\t".htmlspecialchars($this->geterrdesc())."\t".str_replace(array("\r", "\n", "\t"), array(' ', ' ', ' '), trim(htmlspecialchars($sql)))."\n");
			@fclose($fp);
		}

		$message = "<html>\n<head>\n";
		$message .= "<meta content=\"text/html; charset=utf-8\" http-equiv=\"Content-Type\">\n";
		$message .= "<style type=\"text/css\">\n";
		$message .=  "body,p,pre {\n";
		$message .=  "font:12px Verdana;\n";
		$message .=  "}\n";
		$message .=  "</style>\n";
		$message .= "</head>\n";
		$message .= "<body bgcolor=\"#FFFFFF\" text=\"#000000\" link=\"#006699\" vlink=\"#5493B4\">\n";

		$message .= "<p>数据库出错:</p><pre><b>".htmlspecialchars($msg)."</b></pre>\n";
		$message .= "<b>Mysql error description</b>: ".htmlspecialchars($this->geterrdesc())."\n<br />";
		$message .= "<b>Mysql error number</b>: ".$this->geterrno()."\n<br />";
		$message .= "<b>Date</b>: ".date("Y-m-d @ H:i")."\n<br />";
		$message .= "<b>Script</b>: http://".$_SERVER['HTTP_HOST'].getenv("REQUEST_URI")."\n<br />";

		$message .= "</body>\n</html>";
		echo $message;
		exit;
	}
}



@header("content-Type: text/html; charset=GBK");
require_once 'func_db_mysql.php';
$servername="localhost";
$dbusername='alang';
$dbpassword='lingting';
$dbname='phptest';
$usepconnect=0;
$DBQ = new DB_MySQL($servername, $dbusername, $dbpassword, $dbname);
$DBQ->connect();
$rs=$DBQ->query("select *from articles");
 	while($info = $DBQ->fetch_array($rs)){
     	 echo $info['title'];
		 echo $info['content'];
	}
	$rows=$DBQ->num_rows($rs);
$DBQ->close();

你可能感兴趣的:(sql,PHP,mysql,SQL Server,FP)