mysql数据库连接类
这是一款比较常用的mysql+php的数据库连接类文件代码,这款连接类支持pconnect与mysql_connect来连接,用户可以根据自己的WEB应用类型来处理作选择,mysql数据库连接类代码如下:
- $dbhost = 'localhost'; // 数据库服务器
- $dbuser = 'root'; // 数据库用户名
- $dbpw = 'qwaszx'; // 数据库密码
- $dbname = 'movie'; // 数据库名
- $adminemail = 'haha@163.com'; // 系统管理员 Email
- $database = 'mysql'; // 不能修改此处
- $tplrefresh = 1; // 模板自动刷新开关 0=关闭, 1=打开
- $pconnect = 0; // 数据库连接方式 0=connect, 1=pconnect
- class db_class {
- var $querynum = 0;
- //function dbstuff() { global $fp; $fp = fopen("./tempdata/dblog.txt", "w"); }
- 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'");//www.111cn.net
- }
- 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) {
- //echo "|$sql|<br>"; //debug
- //@fwrite($GLOBALS[fp], $sql." "); //debug
- $query = mysql_query($sql);
- //echo 'query:'.$query.'<br>';
- 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;//www.111cn.net
- }
- 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);
- //开源代码phpfensi.com
- $db->select_db($dbname);
- unset($dbhost, $dbuser, $dbpw, $dbname, $pconnect);