In the background of the site, often to deal with the database. This article describes how to use XAMPP to manage MySQL databases and how to access MySQL databases with PHP.
A Using XAMPP to manage MySQL databases
First use XAMPP to open the admin page for MySQL. The steps are as follows: After starting XAMPP click Admin to enter XAMPP for Windows Main page, click phpMyAdmin in the Main page.
After entering the phpMyAdmin page, create a new database test and create a t_student table in this database, table total three fields, number ID, name name, age.
You can then start using PHP to access the MySQL database. Since PHP has been well encapsulated in accessing MySQL databases, accessing MySQL with PHP is a very easy task.
Two PHP Access MySQL Database
The following PHP program accesses the T_student table in the test database, reads the data, and outputs the data in tabular form. All program code is as follows:
by Morewindows (http://blog.csdn.net/MoreWindows)
Defining constants
Define (db_host, ' localhost ');
Define (db_user, ' root ');
Define (Db_pass, ' 111111 ');
Define (db_databasename, ' test ');
Define (db_tablename, ' t_student ');
column names for database tables
$dbcolarray = array (' id ', ' name ', ' age ');
Mysql_connect
$conn = mysql_connect (Db_host, Db_user, Db_pass) or Die ("Connect Failed". Mysql_error ());
mysql_select_db (Db_databasename, $conn);
Reads the number of records in a table
$sql = sprintf ("SELECT count (*) from%s", db_tablename);
$result = mysql_query ($sql, $conn);
if ($result)
{
$count = Mysql_fetch_row ($result);
}
Else
{
Die ("Query failed");
}
echo "table with $count[0] Records
";
$sql = sprintf ("Select%s from%s", Implode (",", $dbcolarray), db_tablename);
$result = mysql_query ($sql, $conn);
Form
Echo '
';//table Header $thstr = "
" . Implode (" |
", $dbcolarray). | ""; echo $thstr;//table contents while ($row =mysql_fetch_array ($result, MYSQL_ASSOC))//with $ROW=MYSQL_FETCH_ASSOC ($result) equivalent {echo '
"; $tdstr = ""; foreach ($dbcolarray as $TD) $tdstr. = "
$row [$TD] | "; Echo $tdstr; echo "
";} echo "
";
Mysql_free_result ($result);
Mysql_close ($conn);
The results of the operation are as follows:
This HTML element is completely output by PHP in the original way, the next article will be used Smarty to complete the task of reading the database and add the "jquery table add delete and modify and set the odd and even row color" in the Set table parity Line Color function added.
Excerpted from Morewindows
http://www.bkjia.com/PHPjc/478477.html www.bkjia.com true http://www.bkjia.com/PHPjc/478477.html techarticle in the background of the site, often to deal with the database. This article describes how to use XAMPP to manage MySQL databases and how to access MySQL databases with PHP. A Use XAMPP to manage MySQL data ...