php连接数据库并动态显示数据库记录
连接到一个 MySQL 数据库
在您能够访问并处理数据库中的数据之前,您必须创建到达数据库的连接。
在 PHP 中,这个任务通过 mysql_connect() 函数完成。
语法
mysql_connect(servername,username,password);
在下面的例子中,我们在一个变量中 ($con) 存放了在脚本中供稍后使用的连接。如果连接失败,将执行 "die" 部分:
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy1326')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1326><?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// some code
?>
有了上面基础之后我们就可以查询数据库并显示了
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4376')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4376>
<?php
mysql_connect("localhost","root","");
mysql_select_db("cinema");
$_sql="select * from `t_hall`";
mysql_query("set names utf8");//编码设置为utf8
$query=mysql_query($_sql);
echo "<table align='center' border='1' cellspacing='0' width='550' >";
echo "<tr><td>影厅编号</td><td>大厅名</td><td>座位行</td><td>座位列</td></tr>";
while($row=mysql_fetch_array($query))
{
//从数据库查询出来的字段
//将数据放到html的表格中
echo "<tr onclick='GetText()'><td>{$row['HID']}</td><td>{$row['HHall']}</td><td>{$row['HSeatline']}</td><td>{$row['HSeatrow']}</td></tr>";
}
echo "</table>";
?>
您可能感兴趣的文章:
php连接数据库并动态显示数据库记录
php数据库连接、查询、显示结果的小例子
php操作数据库的简单示例
PHP MYSQL开发 视频教程
php连接mysql发送sql查询
php中连接数据库的方法是什么?
用连接池提高Servlet访问数据库的效率
php能和sql连接吗?
ADO技术简介
关于mysql操作的详细总结以及实例(一)