【问题标题】:why i cannot call the procedure from php?为什么我不能从 php 调用程序?
【发布时间】:2015-04-27 22:15:39
【问题描述】:

我想从 PHP 调用一个过程,但我做不到。 这是Mysql代码:

 CREATE DEFINER=`zygycoil`@`localhost` PROCEDURE `test`(OUT `a` INT, IN `b`INT, IN `c` INT)
NOT DETERMINISTIC MODIFIES SQL DATA SQL SECURITY DEFINER SET a=c+b

函数所要做的就是在变量中返回 2 的结果(这只是我需要做的一个示例)。

这是我的 php 代码

<?php

if ($mysqli->query("SET @x := 1;") &&  $mysqli->query("SET @y := 1;")  &&   $mysqli->query("SET @msg := 0;") ){
if (!$res = $mysqli->query("CALL test(@x,@y,@msg)", MYSQLI_USE_RESULT)) {
 //if (!$mysqli->query("CALL test(@x,@y)")) {
echo "CALL failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
} else {
echo "CALL failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
while($row = $res->fetch_assoc()){
 var_dump($row);
}
if ($res = $mysqli->query("SELECT @msg")) {
   while($row = $res->fetch_assoc()){
  print_r ($row);
}
}
?>

我在谷歌上看了很多,但无法弄清楚问题出在哪里。然后我添加了$mysqli-&gt;errno,我得到了这个错误:

 "CALL failed: (1414) OUT or INOUT argument 1 for routine zygycoil_data.test is not a variable or NEW pseudo-variable in BEFORE trigger"

【问题讨论】:

标签: php mysql mysqli


【解决方案1】:

我不确定,但试试这个方法:

if ($mysqli->query("SET @x := 1;") &&  $mysqli->query("SET @y := 1;"  &&   $mysqli->query("SET @msg := 0;") {
   if (!$res = $mysqli->query("CALL test(@x,@y,@msg)", MYSQLI_USE_RESULT)) {
   //if (!$mysqli->query("CALL test(@x,@y)")) {
    echo "CALL failed: (" . $mysqli->errno . ") " . $mysqli->error;
    }
} else {
    echo "CALL failed: (" . $mysqli->errno . ") " . $mysqli->error;
}

现在尝试输出:

if ($res = $mysqli->query("SELECT @msg")) {
    while($row = $res->fetch_assoc()){
      print_r $row;
    }
}

【讨论】:

  • 感谢您的回答,但它没有给我这个语法错误:“CALL failed: (1064) You have an error in your SQL syntax;检查与您的 MySQL 服务器版本相对应的手册在第 1 行的 'SET @y := 1 ; CALL test(@x,@y,@msg)' 附近使用正确的语法
  • 什么是错误信息?尝试if (!$mysqli-&gt;query("CALL test(@x,@y)")) {
  • 不起作用:第二次尝试这是错误:"good1CALL failed: (1318) PROCEDURE zygycoil_data.test 的参数数量不正确;预期 3,得到 2
  • 所以让我们尝试准备第三个参数然后:-) 检查更新
  • 我的错误,我认为第一个看起来不错,你也可以帮助获取吗?应该是“while($row = $res->fetch_object()){ echo $row['msg '];} res 是第二个 if 的结果
【解决方案2】:

好的,感谢 alex,这就是有效的解决方案!

<?php   if ($mysqli->query("SET @x := 1;") &&  $mysqli->query("SET @y := 1;") &&   $mysqli->query("SET @msg := 0;") ){
if (!$res = $mysqli->query("CALL test(@x,@y,@msg)")) {
 //if (!$mysqli->query("CALL test(@x,@y)")) {
  echo "CALL failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
} else {
echo "CALL failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
if ($res = $mysqli->query("SELECT @msg")) {
while($row = $res->fetch_assoc()){
  print_r ($row['@msg']);
}
}
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多