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


how to return count of total records from MySQL using stored procedure which returns a value

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;

带out参数时,它必须是像其他回答显示一样的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 +存储过程,我如何获得访问"输出"价值吗?