Reputation: 7891
When I call the Procedure like so in phpMyAdmin (SQL tab), it works:
CALL `test_discount`('022979', 1101, 1, 'W', 100, @p5); SELECT @p5 AS `discount`;
However when I call it in PHP
like so:
$res = $conn->query("CALL `test_discount`('022979', 1101, 1, 'W', 100, @p5); SELECT @p5 AS `discount`;");
I get a syntax error: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT @p5 AS discount' at line 1
How do I get this to work in PHP?
Upvotes: 1
Views: 468
Reputation: 20076
The issue is that mySQL does not support multiple statements in a single query
call: http://php.net/manual/en/mysqli.quickstart.multiple-statement.php
E.g. Given the following code (from the link above):
<?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;
}
?>
You'll receive the following output:
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
Solution: Use a multi-query:
$mysqli->multi_query( "CALL `test_discount`('022979', 1101, 1, 'W', 100, @p5); SELECT @p5 AS `discount`;" );
EDIT: See also:
Example using multi_query
to call a procedure:
$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, " ";
}
$result->close();
if( $mysqli->more_results() ) echo "<br/>";
}
} while( $mysqli->next_result() );
}
$mysqli->close();
Upvotes: 2