複製程式碼程式碼如下:
class Mysql
{
private $php
class Mysql
{
private $conn; >私人$主機;
私人$用戶名;
私人$密碼;
私有$dbname;
私有$pconnect;
私有$charset;
public function __construct(array $__construct(array $__construct(array $ params = null)
{
if (!empty($params)) {
foreach ($params as $k => $v) {
$this->$k = $v;
}
}
}
公用函數connect()
{
$fun = $this->pconnect ? 'mysql_pconnect' : 'mysql_connect';
? 'mysql_pconnect' : 'mysql_connect';
$this- >conn = $fun($this->主機, $this->使用者名稱, $this->密碼);
$this->conn && $this->query('設定名稱' . $this-> charset);
$this->conn && mysql_select_db($this->dbname, $this->conn);
}
public function getInstance()
{
return $this-this-this >conn;
}
公用函數查詢($sql)
{
return mysql_query($sql, $this->conn);
}
public function fetchOne($sql )
{
$data = $this->fetchRow($sql);
回傳$data[0];
}
公用函數fetchCol($sql)
{
$tmp = $this->fetchAll($sql, MYSQL_NUM);
foreach ($tmp as $v) {
$data[] = $v[0];
}
}
public function fetchRow($sql)
{
$result = $this->query($sql);
$data = mysql_fetch_row($result);
mysql_free_result($結果結果);
回傳$資料;
}
public function fetchAssoc($sql)
{
$result = $this->query($sql);
$data = my_fetch_assocsql_fetch_assocsql ($result);
mysql_free_result($結果);
返回$數據;
}
公共函數fetchAll($sql, $type = MYSQL_ASSOC)
{
$result = $this->query($sql);
while ($tmp = mysql_fetch_array($result, $type)) {
$data[] = $tmp;
}
傳回$資料;
}
public function fetchPairs($sql)
{
$result = $this->query($sql);
while ($tmp = mysql_fetch_row($result)) {
$data[$tmp[0]] = $tmp[1];
}
回傳$資料;
}
公用函數insert($table, array $bind)
{
$cols = array();
$vals = array();
foreach ($bind as $col => $val) {
$cols[] = $col;
$vals[] = $val;
unset($bind[$col]);
}
$sql = "插入"
. $表
。 ' (`' .implode('`, `', $cols) . '`) '
. 'VALUES ('' .implode('', '', $vals) . '')';
$stmt = $this->query($sql, $this->conn);
$result = $this->affectedRows();
回傳$結果;
}
public function getLastInsertId()
{
return mysql_insert_id($this->conn);
}
公用函數affectedRows()
{
return mysql_affected_row()
{
return mysql_affected_row_$f; 🎜>}
公用函數update($table, array $bind, $where = '')
{
$set = array();
foreach ($bind as $col => $ val) {
$set[] = '`' . $col . “`='”。 $val。 “'”;
}
$sql = "更新`"
. $表
。 ''設定'.內爆(', ', $set)
. (($where) ? " WHERE $where" : '');
$stmt = $this->query($sql, array_values($bind) );
$result = $this->affectedRows();
回傳$結果;
}
公用函數delete($table, $where = '')
{
/**
* 建構 DELETE 語句
*/
$sql = "DELETE FROM "
. $表
。 (($where) ? " WHERE $where" : '');
/**
* 執行語句並傳回受影響的行數
*/
$stmt = $this->query($sql);
$結果=$ stmt? mysql_affected_rows($this->conn) : $stmt;
回傳$結果;
}
public function close()
{
$this->conn && mysql_close($this-> conn);
}
?> 以上就介紹了php mysql空間一個php mysql類別可以參考學習熟悉下,包含了php mysql空間方面的內容,希望對PHP教學有興趣的朋友有幫助。