PHP invokes the MySQL stored procedure and returns the value implementation program

Source: Internet
Author: User

。 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.