hootie
hootie

Reputation: 13

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";

?>

Upvotes: 1

Views: 2872

Answers (1)

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

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.

Upvotes: 2

Related Questions