A case for connecting to a database: the function used is
1.mysql_connect (' IP ', ' Database user name ', ' Database Password ') Open a connection to the MySQL server returns a resource handle
2.mysql_select_db (' Database name ') Select MySQL database
3.mysql_query (' query statement ')-send a MySQL query
4.mysql_fetch_array (' Result of query statement ', MySQL indexing method) takes a row from the result set as an associative array, or as a numeric array, or both.
Mysql_both, you will get an array that contains both associative and numeric indexes. Use MYSQL_ASSOC only to get associated index, use Mysql_num to get numeric index only
5.mysql_free_result (query result)-free result memory
6.mysql_close-Close MySQL Connection
Or Die (". Mysql_error ()) throws an error
<?php//page encoding Format header (' Content-type:text/html;charset=utf-8 ');//Open Database server define (' db_host ', ' localhost ');d efine ( ' Db_user ', ' root ');d efine (' db_pwd ', ' root ');d efine (' db_name ', ' school '); [Email Protected]_connect (db_host,db_user,db_pwd) or Die (' Database connection failed '. Mysql_error ());//Select the database @mysql_select_db to be used (db _name) or Die (' database does not exist '. Mysql_error ());//conversion with Database encoding @mysql_query (' SET NAMES UTF8 ') or Die (' coding error '. Mysql_error ());// Write the query statement $query= ' Select*from grade ';//execute the query statement to return the result [email protected]_query ($query, $conn) or Die (' Execute statement error '). Mysql_error ()) ;//Print out the execution result Print_r (mysql_fetch_array ($result, MYSQL_ASSOC));//Release Resource mysql_free_result ($result);//Close Database Mysql_close ( $conn);? >
PHP for learning PHP operation SQL One