<?php <BR>class Mysql <BR>{ <BR>private $conn; <BR>private $host; <BR>private $username; <BR>private $password; <BR>private $dbname; <BR>private $pconnect; <BR>private $charset; <br><br>public function __construct(array $params = null) <BR>{ <BR>if (!empty($params)) { <BR>foreach ($params as $k => $v) { <BR>$this->$k = $v; <BR>} <BR>} <BR>} <br><br>public function connect() <BR>{ <BR>$fun = $this->pconnect ? 'mysql_pconnect' : 'mysql_connect'; <BR>$this->conn = $fun($this->host, $this->username, $this->password); <BR>$this->conn && $this->query('set names ' . $this->charset); <BR>$this->conn && mysql_select_db($this->dbname, $this->conn); <BR>} <br><br>public function getInstance() <BR>{ <BR>return $this->conn; <BR>} <br><br>public function query($sql) <BR>{ <BR>return mysql_query($sql, $this->conn); <BR>} <br><br>public function fetchOne($sql) <BR>{ <BR>$data = $this->fetchRow($sql); <BR>return $data[0]; <BR>} <br><br>public function fetchCol($sql) <BR>{ <BR>$tmp = $this->fetchAll($sql, MYSQL_NUM); <BR>foreach ($tmp as $v) { <BR>$data[] = $v[0]; <BR>} <BR>} <br><br>public function fetchRow($sql) <BR>{ <BR>$result = $this->query($sql); <BR>$data = mysql_fetch_row($result); <BR>mysql_free_result($result); <BR>return $data; <BR>} <br><br>public function fetchAssoc($sql) <BR>{ <BR>$result = $this->query($sql); <BR>$data = mysql_fetch_assoc($result); <BR>mysql_free_result($result); <BR>return $data; <BR>} <br><br>public function fetchAll($sql, $type = MYSQL_ASSOC) <BR>{ <BR>$result = $this->query($sql); <BR>while ($tmp = mysql_fetch_array($result, $type)) { <BR>$data[] = $tmp; <BR>} <BR>return $data; <BR>} <br><br>public function fetchPairs($sql) <BR>{ <BR>$result = $this->query($sql); <BR>while ($tmp = mysql_fetch_row($result)) { <BR>$data[$tmp[0]] = $tmp[1]; <BR>} <BR>return $data; <br><br>} <br><br>public function insert($table, array $bind) <BR>{ <BR>$cols = array(); <BR>$vals = array(); <BR>foreach ($bind as $col => $val) { <BR>$cols[] = $col; <BR>$vals[] = $val; <BR>unset($bind[$col]); <BR>} <BR>$sql = "INSERT INTO " <BR>. $table <BR>. ' (`' . implode('`, `', $cols) . '`) ' <BR>. 'VALUES (\'' . implode('\', \'', $vals) . '\')'; <br><br>$stmt = $this->query($sql, $this->conn); <BR>$result = $this->affectedRows(); <BR>return $result; <BR>} <br><br>public function getLastInsertId() <BR>{ <BR>return mysql_insert_id($this->conn); <BR>} <br><br>public function affectedRows() <BR>{ <BR>return mysql_affected_rows($this->conn); <BR>} <br><br>public function update($table, array $bind, $where = '') <BR>{ <BR>$set = array(); <BR>foreach ($bind as $col => $val) { <BR>$set[] = '`' . $col . "` = '" . $val . "'"; <BR>} <br><br>$sql = "UPDATE `" <BR>. $table <BR>. '` SET ' . implode(', ', $set) <BR>. (($where) ? " WHERE $where" : ''); <br><br>$stmt = $this->query($sql, array_values($bind)); <BR>$result = $this->affectedRows(); <BR>return $result; <BR>} <br><br>public function delete($table, $where = '') <BR>{ <BR>/** <BR>* Build the DELETE statement <BR>*/ <BR>$sql = "DELETE FROM " <BR>. $table <BR>. (($where) ? " WHERE $where" : ''); <br><br>/** <BR>* Execute the statement and <span>!本文来源gaodai#ma#com搞*!代#%^码网5</span><pre>搞gaodaima代码
return the number of affected rows
*/
$stmt = $this->query($sql);
$result = $stmt ? mysql_affected_rows($this->conn) : $stmt;
return $result;
}
public function close()
{
$this->conn && mysql_close($this->conn);
}
}
?>