PHP MySQL space a php MySQL class can refer to learning familiar under

Source: Internet
Author: User
Tags pconnect php mysql
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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.