This article gives you a detailed description of how to call execute MySQL stored procedure in PHP and then return the value returned by the stored procedure, there is a need to understand the students can enter the reference.
。 The method that invokes the stored procedure.
A. If the stored procedure has a in/inout parameter, declare a variable, enter a parameter to the stored procedure, the variable is a pair, a PHP variable (also can not be used, just without PHP variable, there is no way to do dynamic input), a MySQL variable.
B. If the stored procedure has an out variable, declare a MySQL variable. The MySQL variable declaration is special, you must let the MySQL server know the existence of this variable, in fact, the execution of a MySQL statement. into set @mysqlvar = $phpvar;
C. Executes the MySQL variable declaration statement using mysql_query ()/mysql_db_query ().
| 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 in parameter, then its value can have phpar incoming
Cases
Using MYSQLI function instances
We can create a stored procedure in MySQL first
| The code is as follows |
Copy Code |
Mysql> delimiter// Mysql> CREATE PROCEDURE employee_list (out param1 INT) BEGIN SELECT COUNT (*) to 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 |
Employee Listing
$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 ('
error:%d (%s)%sn ", Mysqli_errno ($DBH), Mysqli_sqlstate ($DBH), Mysqli_error ($DBH)); } Print ("
'. '
| employee_id |
Surname |
Firstname |
'); while ($row = $result _set->fetch_object ()) {printf ("
| %s |
%s |
%s |
N ", $row->employee_id, $row->surname, $row->firstname); }} else {printf ("
"); $DBH->close (); } ?> |
The core code is
| The code is as follows |
Copy Code |
$result _set = $dbh->query ("Call Employee_list ($dept _id)") |
The employee_list is our MySQL stored procedure.
http://www.bkjia.com/PHPjc/630711.html www.bkjia.com true http://www.bkjia.com/PHPjc/630711.html techarticle This article gives you a detailed description of how to call execute MySQL stored procedure in PHP and then return the value returned by the stored procedure, there is a need to understand the students can enter the reference. Call Store ...