gpt4 book ai didi

php - MySQL 存储过程返回 0 行

转载 作者:行者123 更新时间:2023-11-29 05:56:15 26 4
gpt4 key购买 nike

当我在 phpMyAdmin(SQL 选项卡)中像这样调用过程时,它有效:

CALL `test_discount`('022979', 1101, 1, 'W', 100, @p5); SELECT @p5 AS `discount`;

但是当我在 PHP 中这样调用它时:

$res = $conn->query("CALL `test_discount`('022979', 1101, 1, 'W', 100, @p5); SELECT @p5 AS `discount`;");

我收到语法错误:您的 SQL 语法有误;查看与您的 MariaDB 服务器版本对应的手册,了解在第 1 行的“SELECT @p5 AS discount”附近使用的正确语法


我如何让它在 PHP 中工作?

最佳答案

问题是 mySQL 不支持单个 query 调用中的多个语句:http://php.net/manual/en/mysqli.quickstart.multiple-statement.php

例如给定以下代码(来自上面的链接):

<?php
$mysqli = new mysqli("example.com", "user", "password", "database");
$res = $mysqli->query("SELECT 1; DROP TABLE mysql.user");
if (!$res) {
echo "Error executing query: (" . $mysqli->errno . ") " . $mysqli->error;
}
?>

您将收到以下输出:

Error executing query: (1064) You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the right syntax
to use near 'DROP TABLE mysql.user' at line 1

解决方案:使用多查询:

$mysqli->multi_query( "CALL `test_discount`('022979', 1101, 1, 'W', 100, @p5); SELECT @p5 AS `discount`;" );

编辑:另见:

使用 multi_query 调用过程的示例:

$mysqli = new mysqli(  "HOST", "USR", "PWD", "DBNAME" );
$ivalue=1;
$res = $mysqli->multi_query( "CALL myproc($ivalue,@x);SELECT @x" );
if( $res ) {
$results = 0;
do {
if ($result = $mysqli->store_result()) {
printf( "<b>Result #%u</b>:<br/>", ++$results );
while( $row = $result->fetch_row() ) {
foreach( $row as $cell ) echo $cell, "&nbsp;";
}
$result->close();
if( $mysqli->more_results() ) echo "<br/>";
}
} while( $mysqli->next_result() );
}
$mysqli->close();

关于php - MySQL 存储过程返回 0 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49117378/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com