Methods of invoking MySQL stored procedures and functions in PHP

Source: Internet
Author: User
mysql| Stored Procedures | functions

Stored procedures and functions are just introduced by MySql5.0. There is no direct support for this operation in PHP. But because of the design of the MySQL PHP API, we can support stored procedures and function calls in the MySQL PHP API in previous versions of PHP.

Call stored procedures and functions in PHP.

1. The method that invokes the stored procedure.

A. If the stored procedure has a in/inout parameter, declares a variable, enters the parameter to the stored procedure, and the variable is a pair of

A PHP variable (also can not be, just do not have PHP variables when 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.

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 passed in.

D. If the stored procedure is.

1. Executes the call procedure () statement.

That is mysql_query ("Call Proceduer ([var1] ...)");

2. If there is a return value, execute the Select @ar and return the execution result.

mysql_query ("Select @var)"

The next action is the same as PHP executing a general MySQL statement. Results can be obtained through functions such as Mydql_fetch_row ().

If the time function. It is OK to execute the Select function directly ().


$host = "localhost";
$user = "root";
$password = "11212";
$db = "samp_db";
$dblink =mysql_connect ($host, $user, $password)
Or Die ("can ' t connect to MySQL");
mysql_select_db ($db, $dblink)
Or Die ("Can ' t select samp_db");
$res =mysql_query ("Set @a= $password", $dblink);
$res =mysql_query ("Call AA (@a)", $dblink);
$res =mysql_query ("Select @a", $dblink);
$row =mysql_fetch_row ($res);
echo $row [0];

Turn from: Dynamic Network production guide www.knowsky.com

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.