Home >Backend Development >PHP Tutorial >Access MySql Database with PHP - Elementary_PHP Tutorial

Access MySql Database with PHP - Elementary_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:47:501058browse

In the background of the website, we often have to deal with the database. This article introduces how to use XAMPP to manage MySql database and how to use PHP to access MySql database.

1. Use XAMPP to manage MySql database

First use XAMPP to open the MySql management page. The steps are as follows: After starting XAMPP, click Admin to enter the main page of XAMPP for Windows, and click phpMyAdmin on the main page.




After entering the phpMyAdmin page, create a new database test and create the t_student table in this database. The table has three fields, number id, name, and age.



Then you can start using PHP to access the MySql database. Since PHP has well encapsulated access to the MySql database, it is very easy to use PHP to access MySql.

2. 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 the form of a table. The entire program code is as follows:

// by MoreWindows( http://blog.csdn.net/MoreWindows )
//Define constants
define(DB_HOST, 'localhost');
define(DB_USER, 'root');
define(DB_PASS, '111111');
define(DB_DATABASENAME, 'test');
define(DB_TABLENAME, 't_student');
//Column name of database table
$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);

//Read the number of records in the 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 "There are $count[0] records in the table
";


$sql = sprintf("select %s from %s", implode(",",$dbcolarray), DB_TABLENAME);
$result = mysql_query($sql, $conn);
//Table
echo '

';
//Header
$thstr = "";
echo $thstr;
//Contents in the table
while ($row=mysql_fetch_array($result, MYSQL_ASSOC))//Equivalent to $row=mysql_fetch_assoc($result)
{
echo "";
$tdstr = "";
foreach ($dbcolarray as $td)
$tdstr .= "";
echo $tdstr;
echo "";
}
echo "
" . implode("", $dbcolarray) . "
$row[$td]
";
mysql_free_result($result);
mysql_close($conn);
The running results are as follows:


This method of outputting all HTML elements from PHP is relatively primitive. The next article will introduce using smarty to complete the task of reading the database and set up the table in "Add, delete and modify jquery tables and set odd and even row colors" Odd and even row color functionality was added.

Excerpted from MoreWindows


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478477.htmlTechArticleIn the background of the website, we often have to deal with the database. This article introduces how to use XAMPP to manage MySql database and how to use PHP to access MySql database. one. Use XAMPP to manage MySql data...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn