Copy CodeThe code is as follows:
Class Mysql
{
Private $conn;
Private $host;
Private $username;
Private $password;
Private $dbname;
Private $pconnect;
Private $charset;
Public function __construct (array $params = null)
{
if (!empty ($params)) {
foreach ($params as $k = = $v) {
$this $k = $v;
}
}
}
Public Function Connect ()
{
$fun = $this->pconnect? ' Mysql_pconnect ': ' mysql_connect ';
$this->conn = $fun ($this->host, $this->username, $this->password);
$this->conn && $this->query (' Set names '. $this->charset);
$this->conn && mysql_select_db ($this->dbname, $this->conn);
}
Public Function getinstance ()
{
return $this->conn;
}
Public Function query ($sql)
{
Return mysql_query ($sql, $this->conn);
}
Public Function Fetchone ($sql)
{
$data = $this->fetchrow ($sql);
return $data [0];
}
Public Function 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 ($result);
return $data;
}
Public Function Fetchassoc ($sql)
{
$result = $this->query ($sql);
$data = Mysql_fetch_assoc ($result);
Mysql_free_result ($result);
return $data;
}
Public Function Fetchall ($sql, $type = MYSQL_ASSOC)
{
$result = $this->query ($sql);
while ($tmp = Mysql_fetch_array ($result, $type)) {
$data [] = $tmp;
}
return $data;
}
Public Function Fetchpairs ($sql)
{
$result = $this->query ($sql);
while ($tmp = Mysql_fetch_row ($result)) {
$data [$tmp [0]] = $tmp [1];
}
return $data;
}
Public Function Insert ($table, array $bind)
{
$cols = Array ();
$vals = Array ();
foreach ($bind as $col = = $val) {
$cols [] = $col;
$vals [] = $val;
Unset ($bind [$col]);
}
$sql = "INSERT into"
. $table
. ' ('. Implode (', ', ', $cols). '`) '
. ' VALUES (\ '. Implode (' \ ', \ ', $vals). '\')';
$stmt = $this->query ($sql, $this->conn);
$result = $this->affectedrows ();
return $result;
}
Public Function Getlastinsertid ()
{
Return mysql_insert_id ($this->conn);
}
Public Function AffectedRows ()
{
Return Mysql_affected_rows ($this->conn);
}
Public Function Update ($table, array $bind, $where = ")
{
$set = Array ();
foreach ($bind as $col = = $val) {
$set [] = ". $col. "` = '" . $val. "'";
}
$sql = "UPDATE"
. $table
. ' SET '. Implode (', ', $set)
. ($where)? "WHERE $where": ");
$stmt = $this->query ($sql, Array_values ($bind));
$result = $this->affectedrows ();
return $result;
}
Public Function Delete ($table, $where = ")
{
/**
* Build the DELETE statement
*/
$sql = "DELETE from"
. $table
. ($where)? "WHERE $where": ");
/**
* Execute the statement and 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);
}
}
?>
The above is introduced in PHP MySQL space a php MySQL class can refer to the learning, including the PHP MySQL space aspects of the content, I hope the PHP tutorial interested in a friend helpful.