老师mysqli,和前面学的pdo那个好用

Original 2019-03-22 10:41:21 257
abstract:connect_error){ die($mysqli->connect_error);}$mysqli->

//1、创建数据库连接对象
$mysqli = new MySQLi("localhost","root","123456","liuyan");
if($mysqli->connect_error){
die($mysqli->connect_error);
}
$mysqli->query("set names 'GBK'");
//2、查询多个数据库表
$sqls = "select * from news limit 10,4;";
$sqls .= "select * from user;";
//3、执行并处理结果
if($res = $mysqli->multi_query($sqls)){
//注意:与$mysqli->query()不同,这里返回的是布尔值
do{
$result = $mysqli->store_result();//这里才真正返回结果集的资源对象,失败则返回false;
while($row = $result->fetch_assoc()){
foreach($row as $key=>$value){
echo "--$value--";
}
echo "


";
}
$result->free();
if($mysqli->more_results()){//判断是否还存在有结果集
echo "----------查询下一张表的数据---------------
";
}
}while($mysqli->next_result());//next_result() 返回 true 或false;
}
//4、关闭数据库连接
$mysqli->close();
?>


Correcting teacher:天蓬老师Correction time:2019-03-22 13:34:49
Teacher's summary:你这是多结果集查询吧, 当然pdo好用了, mysqli了解即可

Release Notes

Popular Entries