。 The method that invokes the stored procedure.
A. If the stored procedure has in/inout parameters, declare a variable, input parameters to the stored procedure, the variable is a pair of PHP variables (also can not, just do not have PHP variables, there is no way to dynamic input), a MySQL variable.
B. If the stored procedure has an out variable, declare a MySQL variable. MySQL variable declaration is more special, you must let the MySQL server know the existence of this variable, in fact, is the implementation of a MySQL statement. into set @mysqlvar = $phpvar;
C. Use mysql_query ()/mysql_db_query () to execute the MySQL variable declaration statement.
| The code is as follows |
Copy Code |
| mysql_query ("set @mysqlvar" = $pbpvar ""); |
In this way, there is a variable in the MySQL server, @mysqlar. If the time in parameter, then its value can have phpar incoming
Cases
Using the Mysqli function example
We can first create a stored procedure in MySQL
| The code is as follows |
Copy Code |
Mysql> delimiter// Mysql> CREATE PROCEDURE employee_list (out param1 INT) -> BEGIN -> SELECT COUNT (*) into the param1 from T; -> End ->// Query OK, 0 rows Affected (0.00 sec) |
Then write the following in PHP
| The code is as follows |
Copy Code |
| <form method= "POST" > <p>enter Department ID: <input type= "text" name= "dept_id" size= "4" > <input type= "Submit" name= "submit" value= "Submit" ><p> </form> <?php $hostname = "localhost"; $username = "root"; $password = "secret"; $database = "prod"; if (IsSet ($_post[' submit ')) { $DBH = new Mysqli ($hostname, $username, $password, $database); /* Check Connection * * if (Mysqli_connect_errno ()) { printf ("Connect failed:%sn", Mysqli_connect_error ()); Exit (); } $dept _id = $_post[' dept_id ']; if ($result _set = $dbh->query ("Call Employee_list ($dept _id)")) { Print (' <table border= "1" width= "30%" > <tr> ". ' <td>Employee_id</td><td>Surname</td><td>Firstname</td></tr> '); while ($row = $result _set->fetch_object ()) { printf ("<tr><td>%s</td><td>%s</td><td>%s</td></tr>n", $row->employee_id, $row->surname, $row->firstname); } } else { printf ("<p>error:%d (%s)%sn", Mysqli_errno ($DBH), Mysqli_sqlstate ($DBH), Mysqli_error ($DBH)); } Print ("</table>"); $DBH->close (); } ?> |
The core code is
| The code is as follows |
Copy Code |
$result _set = $dbh->query ("Call Employee_list ($dept _id)") |
Employee_list is our MySQL stored procedure.