Directory:
1. Connection overview
2. Create a php file for MySQL connection
3. View the connection effect
1. Connection overview
The lamp development model is described above and is installed using Appserv. This is the time to embody the advantages. This section describes the data that is read in the MySQL database directly using PHP and is displayed in the browser.
2. Create a php file for MySQL connection
Enter the Appserv installation directory, this machine is in D:\Program files\appserv, enter the WWW directory, in this new folder school, and in the school folder under the new index.php file, and enter the following code in the file, save close.
1<?PHP2 //Connect to the database, select3 $conn=mysql_connect("localhost", "root", "password");//password change to the root account password set when installing Appserv4 $result=Mysql_db_query("Schooldb", "SELECT * from Student",$conn);5 6 //Get query Results7 $row=Mysql_fetch_row($result);8 Echo' <font face= ' Verdana ' > ';9 Echo' <table border= ' 1 "cellpadding=" 1 "cellspacing=" 2 "> ';Ten One //Show Field names A Echo"</b><tr></b>"; - for($i= 0;$i<Mysql_num_fields($result);$i++) - { the Echo' <td><b> '. - Mysql_field_name($result,$i); - Echo"</b></td></b>"; - } + - Echo"</tr></b>"; + //Navigate to the first record A Mysql_data_seek($result, 0); at //Loop Out Records - while($row=Mysql_fetch_row($result)) - { - Echo"<tr></b>"; - for($i= 0;$i<Mysql_num_fields($result);$i++ ) - { in Echo' <td> '; - Echo $row[$i]; to Echo' </td> '; + } - Echo"</tr></b>"; the } * $ Echo"</table></b>";Panax Notoginseng Echo"</font>"; - the //Freeing Resources + Mysql_free_result($result); A //Close Connection the Mysql_close($conn); +?>
View Code
3. View the connection effect
Enter localhost/school/in the browser to see the following.
4 MySQL connection to PHP