数据库操纵基本流程为:
1、连接数据库服务器
2、选择数据库
3、执行SQL语句
4、处理结果集
5、打印操作信息
其中用到的相关函数有
•resource mysql_connect ( [string server [, string username [, string password [, bool new_link [, int client_flags]]]]] ) 连接数据库服务器
•resource mysql_pconnect ( [string server [, string username [, string password [, int client_flags]]]] ) 连接数据库服务器,长连接
•int mysql_affected_rows ( [resource link_identifier] )取得最近一次与 link_identifier 关联的 INSERT,UPDATE 或 DELETE 查询所影响的记录行数。
•bool mysql_close ( [resource link_identifier] )如果成功则返回 TRUE,失败则返回 FALSE。
•int mysql_errno ( [resource link_identifier] )返回上一个 MySQL 函数的错误号码,如果没有出错则返回 0(零)。
•string mysql_error ( [resource link_identifier] )返回上一个 MySQL 函数的错误文本,如果没有出错则返回 ”(空字符串)。如果没有指定连接资源号,则使用上一个成功打开的连接从 MySQL 服务器提取错误信息。
•array mysql_fetch_array ( resource result [, int result_type] )返回根据从结果集取得的行生成的数组,如果没有更多行则返回 FALSE。
•bool mysql_free_result ( resource result )释放所有与结果标识符 result 所关联的内存。
•int mysql_num_fields ( resource result )返回结果集中字段的数目。
•int mysql_num_rows ( resource result )返回结果集中行的数目。此命令仅对 SELECT 语句有效。要取得被 INSERT,UPDATE 或者 DELETE 查询所影响到的行的数目,用 mysql_affected_rows()。
•resource mysql_query ( string query [, resource link_identifier] ) 向与指定的连接标识符关联的服务器中的当前活动数据库发送一条查询。如果没有指定 link_identifier,则使用上一个打开的连接。如果没有打开的连接,本函数会尝试无参数调用 mysql_connect() 函数来建立一个连接并使用之。查询结果会被缓存
代码如下:
class mysql {<br><br> private $db_host; //数据库主机<BR> private $db_user; //数据库登陆名<BR> private $db_pwd; //数据库登陆密码<BR> private $db_name; //数据库名<BR> private $db_charset; //数据库字符编码<BR> private $db_pconn; //长连接标识位<BR> private $debug; //调试开启<BR> private $conn; //数据库连接标识<BR> private $msg = ""; //数据库操纵信息<br><br> // private $sql = ""; //待执行的SQL语句<br><br> public function __construct($db_host, $db_user, $db_pwd, $db_name, $db_chaeset = 'utf8', $db_pconn = false, $debug = false) {<BR> $this->db_host = $db_host;<BR> $this->db_user = $db_user;<BR> $this->db_pwd = $db_pwd;<BR> $this->db_name = $db_name;<BR> $this->db_charset = $db_chaeset;<BR> $this->db_pconn = $db_pc<mark style="color:transparent">来4源gaodaimacom搞#代%码*网</mark><code>搞代gaodaima码</code>onn;<BR> $this->result = '';<BR> $this->debug = $debug;<BR> $this->initConnect();<BR> }<br><br> public function initConnect() {<BR> if ($this->db_pconn) {<BR> $this->conn = @mysql_pconnect($this->db_host, $this->db_user, $this->db_pwd);<BR> } else {<BR> $this->conn = @mysql_connect($this->db_host, $this->db_user, $this->db_pwd);<BR> }<BR> if ($this->conn) {<BR> $this->query("SET NAMES " . $this->db_charset);<BR> } else {<BR> $this->msg = "数据库连接出错,错误编号:" . mysql_errno() . "错误原因:" . mysql_error();<BR> }<BR> $this->selectDb($this->db_name);<BR> }<br><br> public function selectDb($dbname) {<BR> if ($dbname == "") {<BR> $this->db_name = $dbname;<BR> }<BR> if (!mysql_select_db($this->db_name, $this->conn)) {<BR> $this->msg = "数据库不可用";<BR> }<BR> }<br><br> public function query($sql, $debug = false) {<BR> if (!$debug) {<BR> $this->result = @mysql_query($sql, $this->conn);<BR> } else {<br><br> }<BR> if ($this->result == false) {<BR> $this->msg = "sql执行出错,错误编号:" . mysql_errno() . "错误原因:" . mysql_error();<BR> }<BR> // var_dump($this->result);<BR> }<br><br> public function select($tableName, $columnName = "*", $where = "") {<BR> $sql = "SELECT " . $columnName . " FROM " . $tableName;<BR> $sql .= $where ? " WHERE " . $where : null;<BR> $this->query($sql);<BR> }<br><br> public function findAll($tableName) {<BR> $sql = "SELECT * FROM $tableName";<BR> $this->query($sql);<BR> }<br><br> public function insert($tableName, $column = array()) {<BR> $columnName = "";<BR> $columnValue = "";<BR> foreach ($column as $key => $value) {<BR> $columnName .= $key . ",";<BR> $columnValue .= "'" . $value . "',";<BR> }<BR> $columnName = substr($columnName, 0, strlen($columnName) - 1);<BR> $columnValue = substr($columnValue, 0, strlen($columnValue) - 1);<BR> $sql = "INSERT INTO $tableName($columnName) VALUES($columnValue)";<BR> $this->query($sql);<BR> if($this->result){<BR> $this->msg = "数据插入成功。新插入的id为:" . mysql_insert_id($this->conn);<BR> }<BR> }<br><br> public function update($tableName, $column = array(), $where = "") {<BR> $updateValue = "";<BR> foreach ($column as $key => $value) {<BR> $updateValue .= $key . "='" . $value . "',";<BR> }<BR> $updateValue = substr($updateValue, 0, strlen($updateValue) - 1);<BR> $sql = "UPDATE $tableName SET $updateValue";<BR> $sql .= $where ? " WHERE $where" : null;<BR> $this->query($sql);<BR> if($this->result){<BR> $this->msg = "数据更新成功。受影响行数:" . mysql_affected_rows($this->conn);<BR> }<BR> }<br><br> public function delete($tableName, $where = ""){<BR> $sql = "DELETE FROM $tableName";<BR> $sql .= $where ? " WHERE $where" : null;<BR> $this->query($sql);<BR> if($this->result){<BR> $this->msg = "数据删除成功。受影响行数:" . mysql_affected_rows($this->conn);<BR> }<BR> }<br><br> public function fetchArray($result_type = MYSQL_BOTH){<BR> $resultArray = array();<BR> $i = 0;<BR> while($result = mysql_fetch_array($this->result, $result_type)){<BR> $resultArray[$i] = $result;<BR> $i++;<BR> }<BR> return $resultArray;<BR> }<br><br> // public function fetchObject(){<BR> // return mysql_fetch_object($this->result);<BR> // }<br><br> public function printMessage(){<BR> return $this->msg;<BR> }<br><br> public function freeResult(){<BR> @mysql_free_result($this->result);<BR> }<br><br> public function __destruct() {<BR> if(!empty($this->result)){<BR> $this->freeResult();<BR> }<BR> mysql_close($this->conn);<BR> }<BR> }<BR>
调用代码如下
require_once 'mysql_V1.class.php';<BR> require_once 'commonFun.php';<BR> $db = new mysql('localhost', 'root', '', "test");<br><br> //select 查<BR> $db->select("user", "*", "username = 'system'");<BR> $result = $db->fetchArray(MYSQL_ASSOC);<BR> print_r($result);<BR> dump($db->printMessage());<br><br> //insert 增<BR> //$userInfo = array('username'=>'system', 'password' => md5("system"));<BR> //$db->insert("user", $userInfo);<BR> //dump($db->printMessage());<br><br> //update 改<BR> //$userInfo = array('password' => md5("123456"));<BR> //$db->update("user", $userInfo, "id = 2");<BR> //dump($db->printMessage());<br><br> //delete 删<BR> //$db->delete("user", "id = 1");<BR> //dump($db->printMessage());<br><br> //findAll 查询全部<BR> $db->findAll("user");<BR> $result = $db->fetchArray();<BR> dump($result);<BR>
ps,个人比较喜欢tp的dump函数,所以在commonFun.php文件中拷贝了友好打印函数。使用时将其改为print_r()即可。