Php database operation code (add, delete, modify, query) _ PHP Tutorial

Source: Internet
Author: User
Tags php database
Php database operation code (add, delete, modify, query ). The basic process of database operation is: 1. connect to the database server 2. select the database 3. execute the SQL statement 4. process the result set 5. print the operation information. the related functions used here are the basic procedures for operating the res database:

1. connect to the database server

2. select a database

3. execute SQL statements

4. processing result set

5. print operation information

The related functions used include:

• Resource mysql_connect ([string server [, string username [, string password [, bool new_link [, int client_flags]) connect to the database server
• Resource mysql_pconnect ([string server [, string username [, string password [, int client_flags]) connects to the database server for persistent connection
• Int mysql_affected_rows ([resource link_identifier]) obtains the number of rows affected by the last INSERT, UPDATE, or DELETE query associated with link_identifier.
• Bool mysql_close ([resource link_identifier]) returns TRUE if the call succeeds, and FALSE if the call fails.
• Int mysql_errno ([resource link_identifier]) returns the error number of the previous MySQL function. If no error exists, 0 (zero) is returned ).
• String mysql_error ([resource link_identifier]) returns the error text of the previous MySQL function. If no error occurs, ''(null string) is returned ). If no connection resource number is specified, an error message is extracted from the MySQL server using the previous successfully opened connection.
• Array mysql_fetch_array (resource result [, int result_type]) returns an array generated based on the rows obtained from the result set. If no more rows exist, FALSE is returned.
• Bool mysql_free_result (resource result) releases all memory associated with the result ID result.
• 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 only valid for SELECT statements. To obtain the number of rows affected by INSERT, UPDATE, or DELETE queries, use mysql_affected_rows ().
• Resource mysql_query (string query [, resource link_identifier]) sends a query to the active database in the server associated with the specified connection identifier. If link_identifier is not specified for php, use the previous opened connection. If no connection is enabled, this function will try to call the mysql_connect () function without parameters to establish a connection and use it. The query result is cached.
The code is as follows: Source php Tutorial Center


The code is as follows:

Class mysql {

Private $ db_host; // database host
Private $ db_user; // database login name
Private $ db_pwd; // database logon password
Private $ db_name; // database name
Private $ db_charset; // database Character encoding
Private $ db_pconn; // persistent connection identifier
Private $ debug; // debug enabled
Private $ conn; // Database Connection ID
Private $ msg = ""; // database operation 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 code:". mysql_errno (). "error cause:". 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 code:". mysql_errno (). "error cause:". 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 is successfully inserted. 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 updated successfully. 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 deleted successfully. 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 call code is as follows:

The code is as follows:

Require_once 'MySQL _ V1.class. php ';
Require_once 'commonfun. php ';
$ Db = new mysql ('localhost', 'root', ''," test ");

// Select query
$ Db-> select ("user", "*", "username = 'system '");
$ Result = $ db-> fetchArray (MYSQL_ASSOC );
Print_r ($ result );
Dump ($ db-> printMessage ());

// Insert increment
// $ UserInfo = array ('username' => 'system', 'password' => md5 ("system "));
// $ Db-> insert ("user", $ userInfo );
// Dump ($ db-> printMessage ());

// Update
// $ UserInfo = array ('password' => md5 ("123456 "));
// $ Db-> update ("user", $ userInfo, "id = 2 ");
// Dump ($ db-> printMessage ());

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

// FindAll query all
$ Db-> findAll ("user ");
$ Result = $ db-> fetchArray ();

Dump ($ result );


Listen 1. connect to the database server 2. select database 3. execute SQL statement 4. process result set 5. print operation information. the related functions used are res...

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.