<?php //mysql数据库通用类 class Mysql_Oper { private $hostname='127.0.0.1'; private $username='root'; private $password='52ebook'; private $dbname='b_work'; private $conn; function __construct() { $conn=mysql_connect($this->hostname,$this->username,$this->password); mysql_select_db($this->dbname,$conn); if(!$conn) { echo $conn->error(); die; } //设置编码 mysql_query("SET NAMES 'utf8'"); } //实现数据查询,返回所有数据 public function QueryAll($sql,$resultmode='assoc') { $datatable; $result=mysql_query($sql); $mode=$resultmode; switch($result_mode) { case 'assoc': $mode=MYSQL_ASSOC; break; case 'num': $mode=MYSQL_NUM; break; case 'both': $mode=MYSQL_BOTH; break; } //在mysql驱动中没有fetch_all函数,循环实现 while($row=mysql_fetch_row($result,$mode)) { $datatable[]=$row; } return $datatable; } //返回影响的行数 public function QueryAffected($sql) { mysql_query($sql); return mysql_affected_rows(); } //第一行数据 function QueryFirstRow($sql) { $result=mysql_query($sql); return mysql_fetch_row($result); } //获取上一次插入的id号 function QueryInsertid($sql) { mysql_query($sql); return mysql_insert_id(); } function __destruct() { mysql_close($this->conn); } } ?>