简体   繁体   中英

Call MySQL stored procedure from PHP

I have looked at several examples on how to call a MySQL stored procedure from PHP but none have helped me. The stored procedure works when run inside PHPMyAdmin but I am having trouble calling it from the web.

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

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$result = mysqli_query($conn,"CALL standings_build()");

if (mysqli_query($conn,$sql))
  header('refresh:1; url=schedule_main_scores.php');
else
  echo "failed";

?>

There's 2 problems here.

You're querying twice and using the wrong variable, being $sql instead of $result .

$result = mysqli_query($conn,"CALL standings_build()");

if (mysqli_query($conn,$sql))
    ^^^^^^^^^^^^ calling the query twice
                       ^^^^ wrong variable, undefined

all that needs to be done is this:

if ($result)

and an else to handle the (possible) errors.

Error reporting and mysqli_error($conn) would have been your true friends.

Side note: You really should use proper bracing techniques though, such as:

if ($result){
    echo "Success";
}

else {
    echo "The query failed because of: " . mysqli_error($conn); 
}

It helps during coding also and with an editor for pair matching.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM