PHP调用MySQL存储过程出现Error: Commands out of sync; you can’t run this command now
需要在两次调用存储过程中间关闭连接,然后再重新建立一个数据库连接
<?php
header('Content-type:text/html; charset=utf-8');
//1.传入页码
$pageSize=5;//每页显示数据条目数
$showPage=3;//显示页码的个数
$p=$_GET['p'];//当前页
$page=($p-1)*$pageSize;//当前页第一个要显示的条数
//2.根据页码取出数据:php->mysql处理
$link=mysqli_connect('localhost','root','lidekun9132','users','3306');
if(!$link){
die("连接失败:".mysqli_connect_error);
}
mysqli_query($link,"set names utf8;");
//$sql="select * from users_message limit ".$page.","."$pageSize;";
$sql="call users.select_users_message('$page','$pageSize');";
//echo $sql;
$result=mysqli_query($link,$sql);
$arr = [];
while($row = mysqli_fetch_assoc($result)){
$arr[] = $row;
}
mysqli_free_result($result);
mysqli_close($link);
/*
实现分页
*/
$link=mysqli_connect('localhost','root','lidekun9132','users','3306');
if(!$link){
die("连接失败:".mysqli_connect_error);
}
mysqli_query($link,"set names utf8;");
//$total_sql="select count(*) as c from users_message;";
$total_sql="call users.select_users_count_message();";
$res=mysqli_query($link,$total_sql);
if (!$res) {
printf("Error: %s\n", mysqli_error($link));
exit();
}
$total_result=mysqli_fetch_assoc($res);//获取数据库记录数