PHP calls the MySQL stored procedure and returns the value implementation program _php tutorial

Source: Internet
Author: User
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 ("

'. ' '); while ($row = $result _set->fetch_object ()) {printf (" N ", $row->employee_id, $row->surname, $row->firstname); }} else {printf ("
employee_id Surname Firstname
%s %s %s
");
$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 ...

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