php and mysql are a natural pair. Let me introduce how to call mysql stored procedures in php and execute the returned results. Below I have summarized some commonly used methods of calling MySQL stored procedures in php. Students who need to know more can refer to.
MySQL has only introduced stored procedures since 5.0. Anyway, I have never touched them before when I was working on applications. However, now because it is mainly used for internal systems, many applications use stored procedures. Of course, the front desk sometimes needs to call MySQL stored procedures. , PHP’s MySQL Lib doesn’t seem to support it very well, but I searched for some information. Although there is not much, I still tried to use it. Now I will introduce the method
1. Methods that call stored procedures.
a. If the stored procedure has IN/INOUT parameters, declare a variable and input the parameters to the stored procedure. The variable is a pair, a PHP variable (it is not necessary, but there is no way to perform dynamic input when there is no PHP variable) and a Mysql variable.
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;
c. Use mysql_query()/mysql_db_query() to execute the mysql variable declaration statement.
The code is as follows | Copy code | ||||
|
2. If there is a return value, execute select @ar and return the execution result.
代码如下 | 复制代码 |
mysql_query("select @var)" |
代码如下 | 复制代码 |
$sql = "call test.myproce();"; |
The code is as follows | Copy code | ||||
$conn = mysql_connect('localhost','root','root') or die ("Data connection error!!!");
|
The code is as follows | Copy code |
$returnValue = ''; try { mysql_query ( "set @Return" ); $spname = 'P__Test_GetInfo1'; mysql_query ( "call $spname(@Return, '{$userId}', '{$pwd}')" ) or die ( "[$spname]Query failed:" . mysql_error () ); $result_return = mysql_query ( "select @Return" ); $row_return = mysql_fetch_row ( $result_return ); $returnValue = $row_return [0]; } catch (Exception $e) { echo $e; } echo $returnValue; //Output the variables output from the stored procedure |
类型二:调用带多个输出类型和多个输入类型参数的方法
代码如下 | 复制代码 | ||||
try{ mysql_query("set @Message"); mysql_query("set @Id"); mysql_query("call P__Test_Login(@Message, @Id, '{$userId}', '{$pwd}')", $conn) or die("Query failed:".mysql_error()); $result_mess = mysql_query("select @Message"); $result_uid = mysql_query("select @Id"); $row_mess = mysql_fetch_row($result_mess); $row_uid = mysql_fetch_row($result_uid); $Proc_Error = $row_mess[0]; $uId = $row_uid[0]; } catch( Exception $e ) { echo $e; } echo 'proc return message:'$Proc_Error.' '; //输出来自存储过程中输出的变量 echo 'User id:'.$uId; //获取用户id
|
代码如下 | 复制代码 |
try { } catch ( Exception $e ) { |
代码如下 | 复制代码 | ||||
try {
echo $e; } |
代码如下 | 复制代码 |
//PHP $rows = array (); $db = new mysqli($server,$user,$psd,$dbname); if (mysqli_connect_errno()){ $this->message('Can not connect to MySQL server'); } $db->query("SET NAMES UTF8"); $db->query("SET @Message"); if($db->real_query("call P__Test_GetData2(@Message)")){ do{ if($result = $db->store_result()){ while ($row = $result->fetch_assoc()){ array_push($rows, $row); } $result->close(); } }while($db->next_result()); } $db->close(); print_r($rows); //Procedure …… select * from T1 where …… select * from T2 where …… …… |
Example 4: inout stored procedure of outgoing parameters
代码如下 | 复制代码 |
$sql = " create procedure myproce4(inout sexflag int) begin SELECT * FROM user WHERE sex = sexflag; end; "; mysql_query($sql);//创建一个myproce4的存储过程 $sql = "set @sexflag = 1"; mysql_query($sql);//设置性别参数为1 $sql = "call test.myproce4(@sexflag);"; mysql_query($sql);//调用myproce4的存储过程,在cmd下面看效果 |
Example 5: Stored procedure using variables
The code is as follows | Copy code | ||||||||
$sql = "
begin declare s int default 0;set s=a+b; select s;
"; mysql_query($sql);//Create a myproce5 stored procedure$sql = "call test.myproce5(4,6);"; mysql_query($sql);// |
代码如下 | 复制代码 |
$sql = " create procedure myproce7() begin declare i int default 0; declare j int default 0; while i<10 do set j=j+i; set i=i+1; end while; select j; end; "; mysql_query($sql);//创建一个myproce7的存储过程 $sql = "call test.myproce7();"; mysql_query($sql);// |
Example 6: case syntax
The code is as follows | Copy code | ||||||||||||||||||||||||
$sql = " create procedure myproce6(in score int) begin
Call the stored procedure of myproce8 and see the effect under cmd Example 9: loop statement
Example 10: Delete stored procedure
Related labels:
source:php.cn
Previous article:php mysqli executes sql statement program code in batches_PHP tutorial
Next article:Batch import csv file code of data in php_PHP tutorial
Statement of this Website
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
Latest Articles by Author
Latest Issues
javascript - 求解ReferenceError:can not find variable:setFrameGroupIndex at..
From 1970-01-01 08:00:00
0
0
0
Team collaboration - What should I do if someone needs the feature I wrote as a dependency in git flow?
From 1970-01-01 08:00:00
0
0
0
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
|