Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a MySQL stored procedure from PHP?

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.

like image 407
Kim Avatar asked Jan 13 '09 12:01

Kim


People also ask

What is stored procedure in MySQL PHP?

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.

Why we use stored procedure in PHP?

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.


1 Answers

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);
like image 134
Lab5 - Webdesign Switzerland Avatar answered Oct 29 '22 14:10

Lab5 - Webdesign Switzerland