Home  >  Article  >  php教程  >  How to call mysql stored procedures and functions in php

How to call mysql stored procedures and functions in php

高洛峰
高洛峰Original
2016-12-02 10:50:111816browse

Stored procedures and functions were just introduced in MySql5.0. There is no direct support for this operation in PHP. However, due to the design of the Mysql PHP API, we can support calls to stored procedures and functions in the mysql php api in previous PHP versions.

Call stored procedures and functions in php.

1. Methods that call stored procedures.

a. If the storage procedure has the IN/INOUT parameter, declare a variable and the input parameter is given to the stored procedure, the variable is a pair,

a PHP variable (may not, but there is no PHP variable, there is no way to perform dynamic input), a mysql

Variables.

b. If the stored procedure has an OUT variable, declare a Mysql variable.

The declaration of mysql variables is special. The mysql server must know the existence of this variable. In fact, it means executing a mysql statement.

            Enter set @mysqlvar=$phpvar; Use mysql_query()/mysql_db_query() to execute the mysql variable declaration statement.

                                                                                                                                                                                                                      mysql_query("set @mysqlvar【=$pbpvar]"); If it is an IN parameter, its value can be passed in from phpar.

d. If stored procedure.

1. Execute the call procedure() statement. Q q is mysql_query ("Call Proceduer ([VAR1] ...)");

2. If there is a return value, execute select @AR, and return the execution result. Q mysql_query ("Select @var)"

The next operation is the same as the general MySQL statement executed by PHP. The results can be obtained through functions such as mydql_fetch_row().

If function Just execute 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];



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn