Php connects to the mysql database. Today, I learned how to use php to connect to the mysql database. I will write a blog to summarize it. To use php to connect to the mysql database, we need to connect to the database first.
Today, I learned to use php to connect to the mysql database and write a blog to summarize it.
To use php to connect to the mysql database, we need to connect to the database first. We use the following function to connect
$ SQL _link = mysql_connect ("host name", "login user name", "login user name and password ");
If the connection is successful, a mysql handle is returned.
$ SQL _linkIt is a bridge between php and mysql. through this bridge, we can access mysql.
After entering mysql, we need to select a database to operate mysql. We use the following function to select a database.
Mysql_select_db ("database name", "this parameter is optional ");
Then execute some mysql statements and use the following functions:
$ Result = mysql_query ("SQL statement", "this parameter is optional ");
When executing some mysql statements, there are two situations:
1. no results are returned, such as deleting data and modifying the encoding.
2. there are returned results, such as querying data. at this time, we use a variable to receive the results. For example, the following statement returns a table, which can be considered as a two-dimensional array.
$ Result = mysql_query ("select * from person_list", $ SQL _link); // returns a resource, which is currently a table.
Pass
$ ResultWe can know a lot of information about this table. we need to use corresponding functions to obtain the information we need.
1. this table has several fields.
mysql_num_fields( $result );
2. this table has several records.
mysql_num_rows( $result );
3. the field name of the table.
mysql_field_name( $result, $index );
4. retrieve the array one by one using the index array and execute a pointer offset once.
mysql_fetch_row( $result );
5. get the array one by associating the array, and execute a pointer offset once.
mysql_fetch_assoc( $result );
6. specify the pointer position by yourself.
mysql_data_seek( $result, 3 );
7. sometimes we operate on the database and determine whether the database is successful based on the number of affected rows.
Mysql_affected_rows (); // parameters can be set.
The following is a complete code to demonstrate how to read data (the person_list table in the test database) and display it on the page (it does not handle the page garbled characters ).
"; Elseecho mysql_error ($ SQL _link ). ""; $ db_name = "test"; // the database to connect to. mysql_select_db ($ db_name, $ SQL _link); $ SQL _sentence = "select * from person_list "; // the SQL statement to be executed // $ SQL _sentence = "desc person_list"; // the SQL statement to be executed $ result = mysql_query ($ SQL _sentence, $ SQL _link ); // return a resource, which is currently a table echo ''; for ($ index = 0; $ index <mysql_num_fields ($ result); $ index ++) echo ''; while ($ row = mysql_fetch_row ($ result) {echo''; foreach ($ row as $ value) echo ''; // echo '';} echo'
'. Mysql_field_name ($ result, $ index ).' |
'. $ Value .' |
'. $ Value .' |
';?>
Today, I learned to use php to connect to the mysql database and write a blog to summarize it. To use php to connect to the mysql database, we need to connect to the database first...