My attempts to query MySQL from PHP with a create statement of a store procedure (SP) have all failed. Is this not possible ?
If indeed possible, please give an example.
A stored procedure is a subroutine stored in the database catalog. Applications can call and execute the stored procedure. The CALL SQL statement is used to execute a stored procedure. Stored procedures can have IN , INOUT and OUT parameters, depending on the MySQL version.
A Stored Procedures is a precompiled SQL statement stored in the database for later use. Within a Stored Procedure you can write procedural code that controls the flow of execution. That includes if or else constructs, and error-handling code.
Besides of the privileges, what quite probably might cause your problem is, that actually mysql_query($query) can only work one command per call !
So what you have to do is to split up the commands into sevaral mysql_query($query) -calls.
What i mean is something like this:
$query = "DROP FUNCTION IF EXISTS fnElfProef (accountNr INT)";
mysql_query($query);
$query = "CREATE FUNCTION fnElfProef (accountNr INT)
RETURNS BOOLEAN
BEGIN
DECLARE i, totaal INT DEFAULT 0;
WHILE i < 9 DO
SET i = i+1;
SET totaal = totaal+(i*(accountNr%10));
SET accountNr = FLOOR(accountNr/10);
END WHILE;
RETURN (totaal%11) = 0;
END";
mysql_query($query);
$query = "SELECT * FROM mytable";
mysql_query($query);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With