从PHP在MySQL存储过程中传递多个参数时出错

时间:2018-12-16 07:23:38

标签: php mysql mysqli

我在InsertStudent MySQL中有一个存储过程DB,可以从数据库正常工作, 现在,我通过给所有参数赋予以下错误

php调用上述Sp:
    Error: CALL InsertStudent(Mohd Maaz,455,1,2,0)
    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 'Maaz,455,1,2,0)' at line 1

这是我的代码

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "funed";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$StudentName = "Mohd Maaz";
$StudentClass = 1;
$StudentRollNo = 455;
$StudentSection = 2;
$StudentIsdltd = 0;


$sql = "CALL InsertStudent(".$StudentName.",".$StudentRollNo.",".$StudentClass.",".$StudentSection.",".$StudentIsdltd.")";
if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
} 

$conn->close();
?>

1 个答案:

答案 0 :(得分:0)

您只是忘了引用参数

替换

$sql = "CALL InsertStudent(".$StudentName.",".$StudentRollNo.",".$StudentClass.",".$StudentSection.",".$StudentIsdltd.")";

$sql = "CALL InsertStudent('".$StudentName."','".$StudentRollNo."','".$StudentClass."','".$StudentSection."','".$StudentIsdltd."')";

或者我更喜欢直接使用字符串中的变量而不进行连接

$sql = "CALL InsertStudent('$StudentName','$StudentRollNo','$StudentClass','$StudentSection','$StudentIsdltd')";