delimiter //
create procedure sp_AttendReportCountWorkouts(OUT cnt INT)
begin select count(*) into cnt from workout_details;
end;
我已经在 MySQL 中创建了上述存储过程,我正在尝试计算记录数,但无法获得所需的结果。以下是我的 PHP 页面上的实际代码。
$link = mysqli_connect('localhost', 'root', '', 'icoachswim_sp');
if (!$link)
{
printf("Can't connect to MySQL Server. Errorcode: %s\n", mysqli_connect_error());
exit;
}
if ($count = mysqli_query($link,"call sp_AttendReportCountWorkouts()"))
{
}
最佳答案
在您的示例中,$count 只是对 MySQL 结果的引用。 以下是如何处理该引用并获得实际结果的想法。
if($result = mysqli_query($link,"call sp_AttendReportCountWorkouts()"))
{
$row = mysqli_fetch_array($result);
$count = $row[0];
}
更新:这只是一个示例,假设存储过程未使用 out 参数:
create procedure sp_AttendReportCountWorkouts()
begin
select count(*) from workout_details;
end;
如果有输出参数,则必须是像其他答案显示一样的 multi_query 或两个连续调用:
$spResult = mysqli_query($link,"call sp_AttendReportCountWorkouts(@cnt)"));
// process $spResult here before executing next query
$cntResult = mysqli_query($link,"select @cnt"));
关于php - 如何使用返回值的存储过程从MySQL返回总记录数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6941993/