Simple PHP Database Operation class Code (add, delete, change, check) _php instance

Source: Internet
Author: User

The basic process of database manipulation is:

1. Connecting to the database server

2. Select Database

3. Execute SQL statement

4. Processing result set

5, printing operation information

The related functions used are

Resource mysql_connect ([string server [, String username [, string password [, bool new_link [, int client_flags]]] Connecting to a database server
Resource Mysql_pconnect ([string server [, String username [, string password [, int client_flags]]]) connecting to the database server, long connection
int Mysql_affected_rows ([resource link_identifier]) gets the number of record rows that were affected by the most recent link_identifier or DELETE query associated with insert,update.
bool Mysql_close ([resource link_identifier]) returns TRUE if successful, and returns FALSE if it fails.
int Mysql_errno ([resource link_identifier]) returns the error number of the previous MySQL function and returns 0 (0) If there is no error.
string Mysql_error ([resource link_identifier]) returns the error text of the previous MySQL function, and returns ' (an empty string) if there is no error. If you do not specify a connection resource number, the error message is fetched from the MySQL server using the last successfully opened connection.
array mysql_fetch_array (resource result [, int result_type]) returns an array generated from rows obtained from the result set, or FALSE if there are no more rows.
bool Mysql_free_result (resource result) releases all the memory associated with results of the resulting identifier.
int Mysql_num_fields (Resource results) returns the number of fields in the result set.
int Mysql_num_rows (Resource results) returns the number of rows in the result set. This command is valid only for SELECT statements. To obtain the number of rows affected by the Insert,update or DELETE query, use Mysql_affected_rows ().
resource mysql_query (string query [, resource Link_identifier]) sends a query to the currently active database in the server associated with the specified connection identifier. If Link_identifier is not specified, the previous open connection is used. If there is no open connection, this function attempts to call the mysql_connect () function without arguments to establish a connection and use it. Query results will be cached
The code is as follows:


Copy Code code as follows:

Class MySQL {

Private $db _host; Database Host
Private $db _user; Database login Name
Private $db _pwd; Database login Password
Private $db _name; Database name
Private $db _charset; Database character encoding
Private $db _pconn; Long connection Identification bit
Private $debug; Debug Open
Private $conn; Database connection Identification
Private $msg = ""; Database manipulation Information

Private $sql = ""; SQL statements to be executed

Public function __construct ($db _host, $db _user, $db _pwd, $db _name, $db _chaeset = ' UTF8 ', $db _pconn = False, $debug = False ) {
$this->db_host = $db _host;
$this->db_user = $db _user;
$this->db_pwd = $db _pwd;
$this->db_name = $db _name;
$this->db_charset = $db _chaeset;
$this->db_pconn = $db _pconn;
$this->result = ';
$this->debug = $debug;
$this->initconnect ();
}

Public Function Initconnect () {
if ($this->db_pconn) {
$this->conn = @mysql_pconnect ($this->db_host, $this->db_user, $this->db_pwd);
} else {
$this->conn = @mysql_connect ($this->db_host, $this->db_user, $this->db_pwd);
}
if ($this->conn) {
$this->query ("SET NAMES"). $this->db_charset);
} else {
$this->msg = "DB connection error, error number:". Mysql_errno (). "Error Reason:". Mysql_error ();
}
$this->selectdb ($this->db_name);
}

Public Function Selectdb ($dbname) {
if ($dbname = = "") {
$this->db_name = $dbname;
}
if (!mysql_select_db ($this->db_name, $this->conn)) {
$this->msg = "Database Unavailable";
}
}

Public Function query ($sql, $debug = False) {
if (! $debug) {
$this->result = @mysql_query ($sql, $this->conn);
} else {

}
if ($this->result = = False) {
$this->msg = "SQL execution error, wrong number:". Mysql_errno (). "Error Reason:". 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 = "Data insert succeeded. The newly inserted ID is: ". 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 = "Data update succeeded. Number of affected rows: ". 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 = "Data deletion succeeded. Number of affected rows: ". 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);
}
}

The calling code is as follows

Copy Code code as follows:

Require_once ' mysql_V1.class.php ';
Require_once ' commonfun.php ';
$db = new MySQL (' localhost ', ' root ', ' ', ' test ');

Select Check
$db->select ("User", "*", "username = ' System '");
$result = $db->fetcharray (MYSQL_ASSOC);
Print_r ($result);
Dump ($db->printmessage ());

Insert Increase
$userInfo = Array (' username ' => ' system ', ' Password ' => MD5 ("system"));
$db->insert ("user", $userInfo);
Dump ($db->printmessage ());

Update change
$userInfo = Array (' Password ' => MD5 ("123456"));
$db->update ("User", $userInfo, "id = 2");
Dump ($db->printmessage ());

Delete deletion
$db->delete ("user", "id = 1");
Dump ($db->printmessage ());

FindAll Query All
$db->findall ("user");
$result = $db->fetcharray ();
Dump ($result);

PS, the individual prefers the dump function of TP, so the friendly print function is copied in the commonfun.php file. When used, it can be changed to Print_r ().

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.