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

Source: Internet
Author: User
Tags php database
The basic process for database manipulation is:

1. Connect to the database server

2. Select Database

3. Execute SQL statements

4. Processing result Sets

5. Printing operation Information

The correlation functions used are

Resource mysql_connect ([string server [, String username [, string password [, bool new_link [, int client_flags]]]) Connecting to the database server
Resource Mysql_pconnect ([string server [, String username [, string password [, int client_flags]]]) Connect to database server, long connection
int Mysql_affected_rows ([resource link_identifier]) gets the number of record rows 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 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 "(empty string) if there is no error. If no connection resource number is specified, the error message is extracted from the MySQL server using the last successfully opened connection.
array mysql_fetch_array (resource result [, int result_type]) returns an array based on the rows obtained from the result set and returns FALSE if there are no more rows.
bool Mysql_free_result (resource result) frees all memory associated with result identifier results.
int Mysql_num_fields (resource result) returns the number of fields in the result set.
int mysql_num_rows (resource result) returns the number of rows in the result set. This command is valid only for SELECT statements. To get 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 are cached
The code is as follows:


Copy CodeThe code is 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; Commissioning Open
Private $conn; Database Connection Identity
Private $msg = ""; Database manipulation Information

Private $sql = ""; SQL statement 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 = "Database connection error, error number:". Mysql_errno (). "Cause of error:". 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, error number:". Mysql_errno (). "Cause of error:". 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 insertion 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 rows affected: ". 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 rows affected: ". 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 the 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 Add
$userInfo = Array (' username ' = ' system ', ' password ' = ' MD5 ("system"));
$db->insert ("user", $userInfo);
Dump ($db->printmessage ());

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

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

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

PS, individuals prefer the dump function of TP, so a friendly print function is copied in the commonfun.php file. Change it to Print_r () when you use it.

  • Related Article

    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.