通过PHP调用MySQL存储过程时出错

时间:2012-12-05 22:43:40

标签: php mysql stored-procedures

我试图从MySQL调用存储过程并获取两个OUT参数(@eset和@leng)。我想将这两个参数回显给JavaScript,我有一个XMLHttpRequest等待结果。

我收到此错误:

Strict standards: mysqli::next_result(): There is no next result set. 

这是我的代码:

<?php


//get the q parameter from URL
$q=$_GET["q"];
$eset= "";
$length= 0;

// Opens a connection to a MySQL server

$db= new mysqli('localhost', 'db_name', 'pass');
if (!$db) {  die('Not connected : ' . mysql_error());} 

// Set the active MySQL database

$db_selected = $db->select_db('db_name');
if (!$db_selected) {
  die ('Can\'t use db : ' . mysql_error());
} 

// Select all the rows in the markers table

$db->multi_query( "CALL  mst2($q, @eset, @leng);SELECT @eset as eset;SELECT @leng as length" );
$db->next_result();            // flush the null RS from the call
$eset=$db->store_result();       // get the RS containing the id
//echo $eset->fetch_object()->eset, "\n";
$length= $db->store_result();
//echo $length->fetch_object()->leng, "\n";
$response= $eset.$length;
//$eset->free();
//$length->free();


  //$response=str_shuffle($q);

//output the response
echo $response;
?>

1 个答案:

答案 0 :(得分:0)

我假设你的存储过程的第一个参数是VARCHAR,所以第一个问题是你在查询中没有引号传递$q变量。它应该是这样的:

$db->multi_query("CALL mst2('$q', @eset, @leng); SELECT @eset as eset; SELECT @leng as length");

此外,您不需要进行两次SELECT调用,只需执行一次:

SELECT @eset AS eset, @leng AS leng;

不用说,用户输入永远不应该被信任。你应该使用准备好的陈述:

if (($stmt = $db->prepare("CALL mst2(?, @eset, @leng)"))) {
    $stmt->bind_param("s", $q);
    $stmt->execute();
    $stmt->close();

    if (($res = $db->query("SELECT @eset AS eset, @leng AS leng"))) {
        list($eset, $leng) = $res->fetch_array();
        $result = $eset.$length;
        echo $result;

        $res->free();
    }
}