php - 通过PHP调用MySQL存储过程报错

标签 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;
?>

最佳答案

我假设您的存储过程的第一个参数是 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();
    }
}

关于php - 通过PHP调用MySQL存储过程报错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13733873/

相关文章:

php - javascript子串提取

php - 在PDO mysql连接中,pdo查询输出对于mysql null结果集来说什么都没有

sql - 存储过程和用户​​定义函数之间的区别

PHP/mySQL Insert 然后调用存储过程获取id

php - 我应该还是不应该使用 getter 和 setter 方法?

php - jQuery Masonry - 流体布局问题

PHP Paypal,没有账户?

php - 将从mysql获取的数据以逗号分隔排列并计数

mySql-从文件导入数据时出错

java - 使用 pwdencrypt 时登录 从哪里开始?