PHP5 ,MYSQL数据库操作函数封装类集合
相信用过PHP的用户都用过这类的封装类吧,网上也有很多类似的发布,不过各有各长现在我就把我本人的发布出来,供PHP初学者使用,也欢迎专家批评指正,在此谢过了
只能PHP5下使用,PHP4的话可以修改以后再用吧。
<!--p //By CG class db { //public public static $conn; public static $data; public static $fields; public static $row; public static $row_num; public static $insertid; public static $version; public static $affected_rows; public static $query_num = 0; public static $debug = false; //private private static $user; private static $pass; private static $host; private static $db; $host 服务器地址 $user 用户名 $pass 密码 $db 数据库名称 //构造函数 function __construct($host,$user,$pass,$db) { self::$host = $host; self::$pass = $pass; self::$user = $user; self::$db = $db; self::Connect(); }//__construct() //析构函数 function __destruct() { self::close(); }//__destruct() public static function close() { if(self::$conn!=NULL) mysql_close(self::$conn); } //connect public static function Connect() { self::$conn = mysql_connect(self::$host,self::$user,self::$pass) or self::msg("连接数据库失败!可能是mysql数据库用户名或密码不正确"); self::selectdb(self::$db); } public static function query($sql) { $query = mysql_query($sql,self::$conn) or self::msg("SQL语法错误:".htmlspecialchars($sql)); if(self::$debug) { echo $sql . "n"; } self::query_num(); return $query; } public static function fetch_array($query) { self::$data = mysql_fetch_array($query); return self::$data; } //这里返回全部的key public static function fetch_assoc($query) { self::$data = mysql_fetch_assoc($query , MYSQL_ASSOC); return self::$data; }//这里只返回key索引 public static function fetch_num($query) { self::$data = mysql_fetch_num($query , MYSQL_NUM); return self::$data; }//这里只返回数字索引 public static function num_fields($query) { self::$fields = mysql_num_fields($query); return self::$fields; } public static function fetch_row($query) { self::$row = mysql_fetch_row($query); return self::$row; } public static function num_rows($query) { self::$row_num = mysql_num_rows($query); return self::$row_num; } public static function insert_id() { self::$insertid = mysql_insert_id(); return self::$insertid; } public static function affected_rows() { self::$affected_rows = mysql_affected_rows(self::$conn); return self::$affected_rows; } public static function fetch_one_array($sql){ $query = self::query($sql); self::$data = self::fetch_array($query); return self::$data; } public static function free($data) { if($data) mysql_free_result($data); } private static function query_num(){ self::$query_num++; } private static function selectdb($db) { mysql_select_db($db,self::$conn) or self::msg("未找到指定数据库!"); } private static function version() { self::$version = mysql_get_server_info(); return self::$version; } private static function geterror() { return mysql_error(); } private static function geterrno() { return intval(mysql_errno()); } private static function error($info) { echo $info; echo ""; } private static function msg($info) { echo "<strong>错误信息:</strong>"; echo "<b>Mysql error:</b>".self::geterror().""; echo "<b>Mysql error number:</b>".self::geterrno()."n"; echo "<b>Time</b>: ".gmdate("Y-n-j g:ia", time() + (8 * 3600))."n"; echo "<b>Script</b>: ".$_SERVER['PHP_SELF']."n"; exit; } } --> |
私藏的该顶,记号一下,以后肯定用得着
已经加上你的链接 :)
怎样使用呀???杂的不写上呢?
把这些代码保存为一个文件,使用时候导入它,调用时 db->方法名(参数列表)就可以使用了