PHP操作mysql(CRUD)增、删、改、查类
<?php /** * 数据库操作基类 * @author yanli_xu@163.com * */ class DB { /** * 执行插入操作的方法 * @param $table : 表名 * @param $values : 数组数据 */ function insert($table, $values) { $sql = "INSERT INTO {$table}"; $keysql = ''; $valuesql = ''; foreach ( $values as $k => $v ) { $keysql .= "{$k},"; if (preg_match ( "/^[0-9]+$/", $v )) { $valuesql .= "{$v},"; } else { $valuesql .= "'{$v}',"; } } $sql = $sql . '(' . substr ( $keysql, 0, - 1 ) . ')VALUES(' . substr ( $valuesql, 0, - 1 ) . ')'; return $this->query ( $sql ); } /** * 执行删除操作的方法 * @param $table : 表名 * @param $where : 删除条件 */ function delete($table, $where = '') { $sql = "DELETE FROM {$table}"; if ($where != '') $sql .= " WHERE {$where}"; return $this->query ( $sql ); } /** * 执行更新操作的方法 * @param $table : 表名 * @param $values : 数组数据 * @param $where : 更新条件 */ function update($table, $values, $where) { $sql = "UPDATE {$table} SET "; foreach ( $values as $k => $v ) { if (preg_match ( "/^[0-9]+$/", $v )) { $sql .= "$k={$v},"; } else { $sql .= "$k='{$v}',"; } } $sql = substr ( $sql, 0, - 1 ); $sql .= " WHERE {$where}"; return $this->query ( $sql ); } /** * 执行查询操作 * @param $what : 查询字段 * @param $table : 目的表 * @param $where : 查询条件 * @param $orderby : 查询排序 * @param $limit : 查询数量 */ function select($what, $table, $where = '', $orderby = '', $limit = '') { $sql = "SELECT {$what} FROM {$table}"; if ($where != '') $sql .= " WHERE {$where}"; if ($orderby != '') $sql .= " ORDER BY {$orderby}"; if ($limit != '') $sql .= " LIMIT {$limit}"; return $this->fetch_array ( $this->query ( $sql ) ); } function query($sql) { $this->_query ( $sql ); } function fetch_array() { return $this->_fetch_array (); } function num_rows() { return $this->_num_rows (); } function close() { $this->_close (); } } ?>
欢迎大家阅读《PHP操作Mysql(CRUD)增、删、改、查种》,跪求各位点评,by 搞代码
<?php class Mysql extends DB { var $link_id; var $result; function Mysql() { $conn = mysql_connect ( SERVER, USERNAME, PASSWORD ) or die ( "连接失败" ); mysql_select_db ( DATABASE_NAME, $conn ); mysql_query ( "SET NAMES 'gbk'", $conn ); $this->link_id = $conn; } function query($sql) { return $this->result = mysql_query ( $sql, $this->link_id ); } function _fetch_array() { $data = array (); while ( $row = mysql_fetch_array ( $this->result ) ) { array_push ( $data, $row ); } $this->close (); return $data; } function _num_rows() { return mysql_num_rows ( $this->result ); } function _close() { //mysql_free_result ( $this->result ); //mysql_close ( $this->link_id ); } } ?>
微信 赏一包辣条吧~
支付宝 赏一听可乐吧~