Home >Backend Development >PHP Tutorial >How to call mysql stored procedures and functions in php_PHP tutorial
Stored procedures and functions were just introduced in MySQL (the best combination with PHP) 5.0. There is no direct support for this operation in php(as the current mainstream development language). However, due to the design of MySQL(the best combination with PHP) php(as the current mainstream development language) API, we can do it in the previous php( MySQL in the version (the best combination with PHP) php (as the current mainstream development language) api supports stored procedures and function calls.
Call stored procedures and functions in php(as the current mainstream development language).
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,
It’s not necessary, but without php(as the current mainstream development language) variables, there is no way to perform dynamic input), a MySQL(the best combination with PHP)
Variables. b. If the stored procedure has an OUT variable, declare a MySQL(best combination with PHP) variable.
MySQL(The best combination with PHP)The declaration of variables is special, and the MySQL(The best combination with PHP) server must know this variable The existence of is actually the execution of a MySQL (the best combination with PHP) statement.
Enter set @MySQL(the best combination with PHP) var=$php (as the current mainstream development language) var ;
c. Use MySQL(the best combination with PHP)_query()/MySQL(the best combination with PHP)_db_query() to execute MySQL(the best combination with PHP) Best combination) variable declaration statement.
MySQL(The best combination with PHP)_query("set @MySQL(The best combination with PHP)var【=$pbpvar】" );
In this way, there is a variable in the MySQL(The best combination with PHP) server, @MySQL(The best combination with PHP)ar. If it is an IN parameter, its value can be passed in with php(as the current mainstream development language)ar.
d. If stored procedure. 1. Execute the call procedure() statement. That is, MySQL(the best combination with PHP)_query("call proceduer([var1]...)");
2. If there is Return value, execute select @ar, and return the execution result. MySQL(The best combination with PHP)_query("select @var)"
The next operation is the same as php(as now The mainstream development language) executes the same MySQL (the best combination with PHP) statement. 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 (The best combination with PHP)_connect($host,$user,$password)
or die("cant connect to MySQL(The best combination with PHP)");
MySQL(The best combination with PHP)_select_db($db,$dblink)
or die("cant select samp_db");
$res =MySQL(The best combination with PHP)_query("set @a=$password",$dblink);
$res=MySQL(The best combination with PHP )_query("call aa(@a)",$dblink);
$res=MySQL(The best combination with PHP)_query("select @a",$ dblink);
$row=MySQL(The best combination with PHP)_fetch_row($res);
echo $row[0];