0305 数据访问

数据访问

第一步:创建一个链接对象
$db=new MySQLi("localhost","root","","0503");      有构造函数  要操作的数据库
第二步:判断链接是否出错

第一种方式
!mysqli_connect_error() or die("链接失败");

第二种方式

if (mysqli_connect_error())    没有错则执行
{
echo "连接失败";
exit();      退出程序
}

第三步:输出查询语句
$sql=("select * from info");

附加

删除语句

$sql = "delete from Info where Code = 'dsad'";   删除时只需要看看运行是否成功就好

增加语句

$sql = "inser into Info values('p001','','','')";
第四步:执行查询语句   查询语句如果执行成功返回结果集对象,如果执行失败返回false
$jieguo=$db->query($sql);
第五步:从结果集中读取数据

var_dump($result->fetch_row());    返回一行数据的数组(索引数组),每次执行返回一条

var_dump($result->fetch_assoc());    返回关联数组,返回一行数组

var_dump($result->fetch_all());     返回二维数组,返回所有数据

var_dump($result->fetch_object());    返回一行数据,对象


if($jieguo)
{
$x=$jieguo->fetch_all();
echo"<table width='100%' border='1' cellpadding='0' cellspacing='0'>";
echo"<tr><td>code</td><td>name</td><td>sex</td><td>nation</td><td>birthday</td></tr>";
foreach($x as $a)
{
echo"<tr><td>$a[0]</td><td>$a[1]</td><td>$a[2]</td><td>$a[3]</td><td>$a[4]</td></tr>" ;
}
echo"</table>";
}

原文地址:https://www.cnblogs.com/wcc731546227/p/5454763.html