这是一款比较常用的mysql+php的数据库连接类文件代码,这款连接类支持pconnect与mysql_connect来连接,用户可以根据自己的WEB应用类型来处理作选择,mysql数据库连接类代码如下:
- $dbhost = 'localhost';
- $dbuser = 'root';
- $dbpw = 'qwaszx';
- $dbname = 'movie';
- $adminemail = 'haha@163.com';
-
- $database = 'mysql';
- $tplrefresh = 1;
- $pconnect = 0;
-
- class db_class {
- var $querynum = 0;
-
-
- function connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect = 0) {
- if($pconnect) {
- if(!@mysql_pconnect($dbhost, $dbuser, $dbpw)) {
- $this->halt('Can not connect to MySQL server');
- }
- } else {
- if(!@mysql_connect($dbhost, $dbuser, $dbpw)) {
- $this->halt('Can not connect to MySQL server');
- }
- }
- mysql_query("SET NAMES 'GB2312'");
- }
-
- function select_db($dbname) {
- return mysql_select_db($dbname);
- }
-
- function fetch_array($query, $result_type = MYSQL_ASSOC){
-
-
- $query = mysql_fetch_array($query, $result_type);
-
- return $query;
- }
-
- function query($sql, $silence = 0) {
-
-
- $query = mysql_query($sql);
-
- if(!$query && !$silence)
- {
- $this->halt('MySQL Query Error', $sql);
- }
- $this->querynum++;
- return $query;
- }
-
- function unbuffered_query($sql, $silence = 0) {
- $func_unbuffered_query = @function_exists('mysql_unbuffered_query') ? 'mysql_unbuffered_query' : 'mysql_query';
- $query = $func_unbuffered_query($sql);
- if(!$query && !$silence) {
- $this->halt('MySQL Query Error', $sql);
- }
- $this->querynum++;
- return $query;
- }
- function get_one($sql){
- $SQL=$sql;
- if(function_exists('mysql_unbuffered_query')){
- $query = mysql_unbuffered_query($SQL);
- }else{
- $query = mysql_query($SQL);
- }
- $this->querynum++;
- if (!$query) $this->halt('Query Error: ' . $SQL);
-
- $rs =& mysql_fetch_array($query, MYSQL_ASSOC);
- mysql_free_result($query);
- return $rs;
- }
-
- function affected_rows() {
- return mysql_affected_rows();
- }
-
- function error() {
- return mysql_error();
- }
-
- function errno() {
- return mysql_errno();
- }
-
- function result($query, $row) {
- $query = @mysql_result($query, $row);
- return $query;www.111cn.net
- }
-
- function num_rows($query) {
- $query = mysql_num_rows($query);
- return $query;
- }
-
- function num_fields($query) {
- return mysql_num_fields($query);
- }
-
- function free_result($query) {
- return mysql_free_result($query);
- }
-
- function insert_id() {
- $id = mysql_insert_id();
- return $id;
- }
-
- function fetch_row($query) {
- $query = mysql_fetch_row($query);
- return $query;
- }
-
- function close() {
- return mysql_close();
- }
- function halt($message = '', $sql = '') {
- echo $message;
- }
-
- }
-
-
-
- $db = new db_class;
- $db->connect($dbhost, $dbuser, $dbpw, $pconnect);
-
- $db->select_db($dbname);
- unset($dbhost, $dbuser, $dbpw, $dbname, $pconnect);
|