public function query($sql, $debug = false) { if (!$debug) { $this->result = @mysql_query($sql, $this->conn); } else {
} if ($this->result == false) { $this->msg = "sql执行出错,错误编号:" . mysql_errno() . "错误原因:" . mysql_error(); } // var_dump($this->result); }
public function select($tableName, $columnName = "*", $where = "") { $sql = "SELECT " . $columnName . " FROM " . $tableName; $sql .= $where ? " WHERE " . $where : null; $this->query($sql); }
public function findAll($tableName) { $sql = "SELECT * FROM $tableName"; $this->query($sql); }
public function insert($tableName, $column = array()) { $columnName = ""; $columnValue = ""; foreach ($column as $key => $value) { $columnName .= $key . ","; $columnValue .= "'" . $value . "',"; } $columnName = substr($columnName, 0, strlen($columnName) - 1); $columnValue = substr($columnValue, 0, strlen($columnValue) - 1); $sql = "INSERT INTO $tableName($columnName) VALUES($columnValue)"; $this->query($sql); if($this->result){ $this->msg = "数据插入成功。新插入的id为:" . mysql_insert_id($this->conn); } }
public function update($tableName, $column = array(), $where = "") { $updateValue = ""; foreach ($column as $key => $value) { $updateValue .= $key . "='" . $value . "',"; } $updateValue = substr($updateValue, 0, strlen($updateValue) - 1); $sql = "UPDATE $tableName SET $updateValue"; $sql .= $where ? " WHERE $where" : null; $this->query($sql); if($this->result){ $this->msg = "数据更新成功。受影响行数:" . mysql_affected_rows($this->conn); } }
public function delete($tableName, $where = ""){ $sql = "DELETE FROM $tableName"; $sql .= $where ? " WHERE $where" : null; $this->query($sql); if($this->result){ $this->msg = "数据删除成功。受影响行数:" . mysql_affected_rows($this->conn); } }
public function fetchArray($result_type = MYSQL_BOTH){ $resultArray = array(); $i = 0; while($result = mysql_fetch_array($this->result, $result_type)){ $resultArray[$i] = $result; $i++; } return $resultArray; }
// public function fetchObject(){ // return mysql_fetch_object($this->result); // }
public function printMessage(){ return $this->msg; }
public function freeResult(){ @mysql_free_result($this->result); }
public function __destruct() { if(!empty($this->result)){ $this->freeResult(); } mysql_close($this->conn); } }
调用代码如下
. 代码如下:
require_once 'mysql_V1.class.php'; require_once 'commonFun.php'; $db = new mysql('localhost', 'root', '', "test");
//select 查 $db->select("user", "*", "username = 'system'"); $result = $db->fetchArray(MYSQL_ASSOC); print_r($result); dump($db->printMessage());
//insert 增 //$userInfo = array('username 上一页 [1] [2] [3] 下一页
|