使用php返回MySQL存储过程结果集

时间:2019-02-19 19:47:47

标签: php mysql

我是php的新手,无法从存储过程中获取结果集,如果可能的话,我希望以过程格式提供答案。

存储过程如下:

DROP PROCEDURE IF EXISTS getJobs;
DELIMITER $$
CREATE PROCEDURE getJobs(IN inputName VARCHAR(50))
BEGIN 
SELECT JobNo FROM jobs WHERE PersonnelName= inputName;
END $$
DELIMITER ;

在MySQL Workbench中,此返回30539

我提出了以下php代码。

<?php
//imports the connection
require "conn.php";

$name = "chrisf";

$call = mysqli_prepare($conn, 'CALL getJobs(?)');
mysqli_stmt_bind_param($call, 's', $name);
mysqli_stmt_execute($call);
$result = mysqli_use_result($conn);

//debug to check the result
echo '<pre>'; print_r($result); echo '</pre>';
//loop the result set and echo
while ($row = mysqli_fetch_array($result)){   
//the command I expect to output the result
  echo "Entry" . $row[0] . "<br>"; 
//debug to check the result
 echo "Entry" .  $row . "<br>"; 
}

$conn->close();
?>

运行时,将提供以下输出:

mysqli_result Object
(
[current_field] => 0
[field_count] => 1
[lengths] => 
[num_rows] => 0
[type] => 1
)

 Entry
 EntryArray

由于如果我使用不返回任何结果的输入,则mysqli_result对象会消失,这似乎很接近,但实际上并不包含数据。

谢谢。

1 个答案:

答案 0 :(得分:0)

我看起来不太优雅,但是我提出了以下内容,这些内容足以让我满意:

<?php
//imports the connection
require "conn.php";

//builds the MySQL stored procedure all
$name = "chrisf";
$input = "CALL getEngineersJob('".$name."')";

//executes the store procedure
$result = mysqli_query($conn,$input) or die("Query fail: " . mysqli_error());

//loop through the output and echo
 while ($row = mysqli_fetch_array($result)){   
   echo $row[0] . "<br>"; 
 }

//free resources
mysqli_free_result($result);
$conn->close();
?>