This article will introduce a good introductory example of PHP mysql to PHP beginners. We connect to the database and display the record examples in the database. Friends can refer to it.
Connect to a MySQL database
Before you can access and process data in the database, you must create a connection to the database.
In PHP, this task is accomplished through the mysql_connect() function.
Grammar
mysql_connect(servername,username,password);
In the example below, we store the connection in a variable ($con) for later use in the script. If the connection fails, the "die" part will be executed:
The code is as follows
代码如下 |
复制代码 |
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// some code
?>
|
|
Copy code
|
代码如下 |
复制代码 |
mysql_connect("localhost","root","");
mysql_select_db("cinema");
$_sql="select * from `t_hall`";
mysql_query("set names utf8");//编码设置为utf8
$query=mysql_query($_sql);
echo "
";
echo "影厅编号 | 大厅名 | 座位行 | 座位列 | ";
while($row=mysql_fetch_array($query))
{
//从数据库查询出来的字段
//将数据放到html的表格中
echo " {$row['HID']} | {$row['HHall']} | {$row['HSeatline']} | {$row['HSeatrow']} | ";
}
echo " ";
?>
|
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
} |
// some code
?>
After having the above foundation, we can query the database and display it
The code is as follows
|
Copy code
mysql_connect("localhost","root","");
mysql_select_db("cinema");
$_sql="select * from `t_hall`";
mysql_query("set names utf8");//Encoding is set to utf8
$query=mysql_query($_sql);
echo "";
echo "A theater number | Hall name | Seat row | Seat row | ";
while($row=mysql_fetch_array($query))
{
//Fields queried from the database
//Put the data into the html table
echo " {$row['HID']} | {$row['HHall']}< /td> | {$row['HSeatline']} | {$row['HSeatrow']} | ";
}
echo " ";
?>
http://www.bkjia.com/PHPjc/628663.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/628663.htmlTechArticleThis article will introduce a good php mysql entry example for php beginners. We connect to the database and display the database Friends can refer to the record examples in . Connect to a...
|
|
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