【问题标题】:store procedures in parameter php error在参数php错误中存储过程
【发布时间】:2017-04-01 23:29:20
【问题描述】:

我有两个存储过程,几乎相同,唯一的区别是第二个有一个 in 参数。

没有参数的 sp 可以正常工作,生成一个空表。我已经在本地服务器上测试了存储过程,所以存储过程本身没有问题。

我还更正了一些在 mysqli 中混合过程和 oop 编码的代码。感谢“gmc”,我纠正了这一点。 这是我的代码:

    //sp

//This store proc without in params is working
echo "<h4>Författartabellen hämtad från databasen med en store procedure</h4>";
  $result_sp = mysqli_query($conn, "CALL jokes");

  //loop the result set
 echo "<table><tr><th>Joke</th><th>AuthorName</th></tr>";
    // output data of each row
    while($row = mysqli_fetch_assoc($result_sp)) {
        echo "<tr><td>".$row["joketext"]."</td><td>".$row["name"]."</td></tr>";
    }
    echo "</table>";

// This store proc with in param is not working, returns a empty table
//run the store proc
echo "<h4>Författartabellen hämtad från databasen med en store procedure</h4>";
  $result_sp_in = mysqli_query($conn, 'CALL jokes_author("Joan Smith")');

  //loop the result set
 echo "<table><tr><th>Joke</th><th>AuthorName</th></tr>";
    // output data of each row
    while($row = mysqli_fetch_array($result_sp_in)) {
        echo "<tr><td>".$row["joketext"]."</td><td>".$row["name"]."</td></tr>";
    }
    echo "</table>";

谢谢

【问题讨论】:

  • 您使用的是mysqli的程序样式,因此您不能将其结果用作对象
  • 所以mysqli的程序风格永远不能用于带参数的存储过程,只能用于不带参数的存储过程呢?那么 mysqli 的 OOP 风格可以用这个吗?
  • 你可以,但你必须确保 assoc 也使用程序样式:$row = mysqli_fetch_assoc($result_sp_in)
  • 谢谢,现在我没有收到任何错误,而是一个空表。我检查了我的 in 参数在表中是否有正确的字符串
  • 您能否展示一下使用 Procedural 样式的代码是什么样子的?

标签: php stored-procedures mysqli


【解决方案1】:

更新:试试这个:

$result_sp_in = mysqli_query($mysqli, 'CALL jokes_author("Joan Smith", @joketext, @name)');
$select = mysqli_query($mysqli, 'SELECT @joketext, @name');
while($row = mysqli_fetch_array($select)) {
    echo "<tr><td>".$row["joketext"]."</td><td>".$row["name"]."</td></tr>";
}
echo "</table>";

【讨论】:

  • 我之前已经更正了我的代码,这是没有参数的 sp。它带有参数的 sp 显示了一个正在使用 $result_sp_in 的空表
  • 所以我应该尝试使用 mysqli_fetch_array 而不是 mysqli_fetch_assoc?如果它不是 mysqli_fetch_array 参数中的 $result_sp_in,我也试过了,它仍然向我显示一个空表
  • 您确定您的 MySQL 程序运行正常吗?
  • 是的,我在我的 mysql localhost 上测试了相同的 proc,参数也相同,并且在数据库本身中调用 sp 时它可以工作。但是 in 参数:'CALL jokes_author("John Smith")' 应该带单引号还是双引号或根本不带引号?
  • 用我写的引号应该可以。试试:$result_sp_in = mysqli_query($conn, 'CALL jokes_author("%John%")');
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-26
  • 1970-01-01
  • 1970-01-01
  • 2013-12-02
相关资源
最近更新 更多