数据访问

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>


<table width="100%" border="1" cellpadding="0" cellspacing="0">

<tr>
<td>代号</td>
<td>姓名</td>
<td>性别</td>
<td>民族</td>
<td>生日</td>
</tr>

<?php

//造一个mysqli对象,造连接对象
$db = new MySQLi("localhost","root","123","mydb");

//准备一条SQL语句
$sql = "select * from info";

//执行SQL语句,如果是查询语句,成功返回结果集对象
$reslut = $db->query($sql);

//判断返回是否执行成功
if($reslut)
{
while($attr = $reslut->fetch_row())
{
echo "<tr>
<td>{$attr[0]}</td>
<td>{$attr[1]}</td>
<td>{$attr[2]}</td>
<td>{$attr[3]}</td>
<td>{$attr[4]}</td>
</tr>";
}

}



?>

</table>






</body>
</html>







<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>

<?php

//造连接对象
$db = new MySQLi("localhost","root","123","mydb");

//准备SQL语句
$sql = "delete from info where code='p004'";

//执行SQL语句
$r = $db->query($sql);

if($r)
{
echo "执行成功";
}
else
{
echo "执行失败";
}


?>

</body>
</html>







<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>



<?php
$db = new MySQLi("localhost","root","123","mydb");
$sql = "select * from nation";
$result = $db->query($sql);

echo "<select>";

if($result)
{
while($attr = $result->fetch_row())
{
echo "<option value='{$attr[0]}'>{$attr[1]}</option>";
}
}

echo "</select>";

?>





</body>
</html>
posted @ 2017-02-23 16:25  周全264  阅读(197)  评论(0编辑  收藏  举报