Home > php教程 > php手册 > body text

php连接数据库并动态显示数据库记录

WBOY
Release: 2016-06-13 10:17:03
Original
1441 people have browsed it

本文章来给php初学者介绍一个不错的php mysql入门实例,我们连接数据库并实现显示出数据库中的记录实例,各位朋友可参考。

连接到一个 MySQL 数据库

在您能够访问并处理数据库中的数据之前,您必须创建到达数据库的连接。

在 PHP 中,这个任务通过 mysql_connect() 函数完成。

语法

mysql_connect(servername,username,password);

在下面的例子中,我们在一个变量中 ($con) 存放了在脚本中供稍后使用的连接。如果连接失败,将执行 "die" 部分:

 代码如下 复制代码

$con = mysql_connect("localhost","peter","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

// some 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 "

";
}
echo "
影厅编号 大厅名 座位行 座位列
{$row['HID']} {$row['HHall']} {$row['HSeatline']} {$row['HSeatrow']}
";
?>
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!