简体   繁体   中英

Issue with Procedure in PHP and MySQL

When I run my program for the first time, I would like to create the following procedure:

$returnCusProcedure = "
    DELIMITER //
    CREATE PROCEDURE GetAllCustomers()
    BEGIN
    SELECT * FROM CUSTOMERS;
    END //
    DELIMITER ; ";

if(! $returnCusProcedure )
{
    die('Could not create procedure: ' . $conn->error);
}
else
{
    echo "Procedure created successfully<br/>";
}

It echos out that the procedure has completed successfully but when I call that procedure in MySQL, it returns that the procedure does not exist, am I missing something from above?

You have to actually execute those SQL statements. It would be something like:

$cusProcedure = "
    CREATE PROCEDURE GetAllCustomers()
    BEGIN
    SELECT * FROM CUSTOMERS;
    END"; // <-- No need to change delimiter, at least in this case

$returnCusProcedure = $conn->query($cusProcedure); // <-- Executing SQL

if(! $returnCusProcedure )
{
    die('Could not create procedure: ' . $conn->error);
}
else
{
    echo "Procedure created successfully<br/>";
}

I was not actually creating the Procedure, only defining it as a string, thanks to @outlyer for bringing that to my attention. I have since edited the code like so and everything is working now:

$returnCusProcedure = "
    DELIMITER //
    CREATE PROCEDURE GetAllCustomers()
    BEGIN
    SELECT * FROM CUSTOMERS;
    END //
    DELIMITER ; ";

$executeQuery = mysqli_query($conn, $returnCusProcedure );

if($conn->query($executeQuery) == TRUE)
{
    die('Could not create procedure: ' . $conn->error);
}
else
{
    echo "Procedure created successfully<br/>";
}

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