php - 如何使用返回值的存储过程从MySQL返回总记录数

标签 php mysql stored-procedures

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/

相关文章:

mysql - 将 json_encode 字符串保存到 mysql 数据库

PHP 通用常量

php - 如何使用 Doctrine 通过 JOIN 填充附加字段

php - 在查询中使用字段名的内容

oracle - Oracle存储过程中的 "AS"和 "IS"有什么区别?

mysql - 将 "Execute statement"结果放入 MySQL 中的变量中

sql-server-2008 - SQL Server 触发器可以向我发送电子邮件吗?

php - PHP如何增加jwt过期时间

php - 使用 PHP 正则表达式从 MySQL 查询中剥离/删除所有值?

php - Mysql 查询对我不起作用 Codeigniter Php