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 wo开发者_开发知识库rkout_details;
end;
I have created the above stored procedure in MySQL and I'm trying to take count of records but I am not able to get the desired result. The following is actual code on my PHP page.
$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()"))
{
}
In your example $count is just a reference to MySQL result. Here is an idea how to process that reference and get the actual result.
if($result = mysqli_query($link,"call sp_AttendReportCountWorkouts()"))
{
$row = mysqli_fetch_array($result);
$count = $row[0];
}
Update: this is just an example assuming the stored procedure is not using out parameter:
create procedure sp_AttendReportCountWorkouts()
begin
select count(*) from workout_details;
end;
With an out paramter is has to be either multi_query like other answer shows or two sequential calls:
$spResult = mysqli_query($link,"call sp_AttendReportCountWorkouts(@cnt)"));
// process $spResult here before executing next query
$cntResult = mysqli_query($link,"select @cnt"));
Here's a previous question that addresses retrieving output variables:
PHP + MySql + Stored Procedures, how do I get access an "out" value?
精彩评论