If an error occurs in the article, I hope you will forgive me and point it out.
To put it bluntly, for PHP, it supports MYSQL by default, that is, we do not have to manually set ODBC to use MYSQL. What we need to do is to introduce the MYSQL plug-in php. ini. First find your php. ini, edit it, and remove the semicolon (";") before extension = php_mysql.dll. In this way, we can use MYSQL for php.
Next, we connect to mysql. mysql_connect (servername: port, username, password) is the function used to connect to mysql. servername: the port parameter is the database address and database port (the port parameter is 3306 by default), the username is the username used to log on to the database, and the password is the password of the corresponding user. Since my database is on the local machine, I use the following statements when connecting.
Connection code
$ Con = mysql_connect ("localhost", "root", ""); // $ con can be understood as the connection number.
Of course, we cannot keep mysql in the connection state. This may not be a problem for small applications, but it is a waste of resources for large web applications, so we need to turn it off after the database connection is used up. We will hand over this task to mysql_close (connection number) to complete.
Enable and disable database connection
| The code is as follows: |
Copy code |
$ Con = mysql_connect ("loaclhost", "root ",""); $ Con = mysql_close ($ con ); |
I figured out how to connect to and disconnect the database. The next step is to create a database in mysql. It sounds very easy because multiple databases can exist on the same mysql server. Here we have two methods to create a database. Method 1 uses the mysql_create_db () function, but this method is not recommended.
Create a database
| The code is as follows: |
Copy code |
$ Con = mysql_connect ("localhost", "root ",""); Mysql_create_db ("guestbook", $ con ); Mysql_close ($ con ); |
Method 2: Use the mysql_query () function to execute SQL statements. Create DATABASE is an SQL statement used to Create a DATABASE. For example, Create DATABASE test creates a DATABASE named test.
Create a database
| The code is as follows: |
Copy code |
$ Con = mysql_connect ("localhost", "root ",""); Mysql_query ("Create DATABASE guestbook", $ con ); Mysql_close ($ con ); |
After the database is created, we need to add data tables to the database. Similarly, the mysql_query () function is used to execute SQL statements. However, the difference is that we used the mysql_select_db () function to select the database for our operation.
| The code is as follows: |
Copy code |
$ Con = mysql_connect ("localhost", "root ",""); Mysql_query ("Create DATABASE guestbook", $ con ); Mysql_select_db ("guestbook", $ con ); Mysql_query ("Create TABLE users (username varchar (15 ), Password varchar (15 ))"); Mysql_close ($ con ); /* TABLE creation statement: Create TABLE indicates (field name_1 field data type _ 1, field name_2 field data type _ 2 ,......, field name_n field data type _ n )*/ |
After the data relationships such as tables and fields are established in the database, the actual data is poor. Same as above, use the mysql_query () function to execute the SQL statement for adding data. Insert INTO table name (field name _ 1, field name _ 2 ,......, field name _ n) VALUES (data _ 1, data _ 2 ,......, data _ n ).
| The code is as follows: |
Copy code |
$ Con = mysql_connect ("localhost", "root ",""); Mysql_query ("Create DATABASE guestbook", $ con ); Mysql_select_db ("guestbook", $ con ); Mysql_query ("Create TABLE users (username varchar (15 ), Password varchar (15 ))"); Mysql_query ("Insert INTO users (username, password) VALUES ('Ku _ Andrew ', '123 ')"); Mysql_close ($ con ); |
Now that we have data in our database, we will use the select statement to query the data.
| The code is as follows: |
Copy code |
$ Con = mysql_connect ("localhost", "root ",""); Mysql_query ("Create DATABASE guestbook", $ con ); Mysql_select_db ("guestbook", $ con ); Mysql_query ("Create TABLE users (username varchar (15 ), Password varchar (15 ))"); $ Select = mysql_query ("select * from users "); While ($ row = mysql_fetch_array ($ select );) { Echo $ row ['username']; Echo $ row ['password']; } Mysql_close ($ con ); |
In this way, through the while loop, we can query all the data in the users Table. The above is not a good way to restore the profits. Below we can make it a database connection class.
| The code is as follows: |
Copy code |
<? Php // Configure //------------------------------------------------------------------------------------------ // ※Database () constructor, initial Database parameter // ※Select () query // ※Getrows () returns the total number of queried records // ※Insert () inserts a record. // ※Update () Update // ※Delete () Delete // ※Halt () is interrupted and error message 111Cn. Net */ // Configure //------------------------------------------------------------------------------------------ Define ("DATABASETYPE", "1"); // defines the database type: 1 for MySql; 2 for SQL Server; 3 for Oracle; 4 for Odbc Define ("SERVER", "localhost"); // Host name or IP address of the database server Define ("DATABASE", "dbName"); // name of the DATABASE to be connected Define ("USER", "tableName"); // USER name used to connect to the database Define ("PASSWORD", "paswd"); // PASSWORD used to connect to the database Class Database { Var $ dbLink; // connection handle Var $ result; // query handle Var $ insId; // Insert () returns the value of the AUTO_INCREMENT column Var $ rows; // an array of returned data Var $ numRows; // number of returned data Var $ dbHost, $ dbUser, $ userPassword, $ database; Var $ dbType = DATABASETYPE; Var $ msgFlag = "yes"; // yes: show the Mysql message; no: die by show "Halted ." Function Database ($ dbHost = SERVER, $ dbUser = USER, $ userPassword = PASSWORD, $ database = DATABASE ){ Switch ($ this-> dbType ){ Case 1: $ This-> dbLink = @ mysql_pconnect ($ dbHost, $ dbUser, $ userPassword); // or die ("Can't Connect to Remote Host! "); @ Mysql_select_db ($ database, $ this-> dbLink); // or die ("Can't Connect to Remote Host! "); Break; Case 2: Break; } Return true; } /* SQL: Select () returns false without result */ Function Select ($ table, $ columns, $ condition = 1 ){ $ SQL = "select $ columns from $ table where $ condition "; $ This-> result = @ mysql_query ($ SQL, $ this-> dbLink ); Unset ($ this-> rows ); If ($ this-> result ){ $ I = 0; If (! ($ This-> rows = array ("$ I" => @ mysql_fetch_array ($ this-> result )))) Return false; If ($ this-> numRows = @ mysql_num_rows ($ this-> result) = 0) Return false; While ($ tempRows = @ mysql_fetch_array ($ this-> result )){ Array_push ($ this-> rows, $ tempRows ); } } Else { $ This-> Halt ($ SQL ); Return false; } Return true; } /* SQL: total number of records returned by GetRows */ Function GetRows ($ table, $ condition = 1 ){ $ SQL = "select count (1) as count from $ table where $ condition "; $ This-> result = @ mysql_query ($ SQL, $ this-> dbLink ); If ($ this-> result ){ $ Temp = @ mysql_fetch_array ($ this-> result ); $ This-> numRows = $ temp [count]; } Else { $ This-> Halt ($ SQL ); Return false; } Return $ this-> numRows; } /* SQL: Insert ()*/ Function Insert ($ table, $ columns, $ values ){ $ SQL = "insert into $ table ($ columns) values ($ values )"; $ This-> result = @ mysql_query ($ SQL, $ this-> dbLink ); If ($ this-> result) $ This-> insId = @ mysql_insert_id ($ this-> dbLink ); Else { $ This-> Halt ($ SQL ); Return false; } Return true; } /* SQL: Update ()*/ Function Update ($ table, $ setings, $ condition ){ $ SQL = "update $ table set $ setings where $ condition "; $ This-> result = @ mysql_query ($ SQL, $ this-> dbLink ); If ($ this-> result) $ This-> numRows = @ mysql_affected_rows ($ this-> result ); Else { $ This-> Halt ($ SQL ); Return false; } Return true; } /* SQL: Delete */ Function Delete ($ table, $ condition ){ $ SQL = "delete from $ table where $ condition "; $ This-> result = @ mysql_query ($ SQL, $ this-> dbLink ); If ($ this-> result) $ This-> numRows = @ mysql_affected_rows ($ this-> result ); Else { $ This-> Halt ($ SQL ); Return false; } Return true; } /* Halt (): error message */ Function Halt ($ msg ){ If ($ this-> msgFlag = "yes "){ Printf ("<B> Database Query Error: </B> % s <br> n", $ msg ); Printf ("<B> MySql Error: </B> % s <br> n", mysql_error ()); } Else Echo "<META HTTP-EQUIV = refresh content = '0; URL = ../include/error.htm '>"; // customize an error prompt file Return false; } } Switch ($ db-> dbType ){ Case 1: @ Mysql_close (); Break; Case 2: Break; } $ Db = new Database (); ?> |