Home >Backend Development >PHP Tutorial >How to get MySQL procedure results through PHP_PHP Tutorial
Netizens always ask me, how to call MySQL's stored procedure through PHP and obtain the returned result set at the same time? Indeed, MySQL's stored procedure greatly facilitates programming and improves efficiency. However, it is troublesome for those students who are still using PHP 4, because PHP 4 can only call stored procedures, but it cannot directly obtain the returned result set; however, it can be done with the mysqli function of PHP 5. First, recompile PHP 5 to add support for mysqli, or download the mysqli extension module directly, which I won’t go into details here. Let’s give an example directly:
1. Create a stored procedure and list all tables under the test library:
<p>mysql>DELIMITER //</p><p>mysql>CREATE PROCEDURE `yejr`()</p><p>->BEGIN</p><p>->SHOW TABLES;</p><p>->END; //</p><p>Query OK, 0 rows affected (0.12 sec)</p><p>mysql>DELIMITER ;</p><p>mysql>CALL yejr();</p><p>+------------------+</p><p>| Tables_in_test |</p><p>+------------------+</p><p>| yejr1 |</p><p>| yejr2 |</p><p>+------------------+</p> |
<p>$mysqli = new mysqli("localhost", "root", "", "test");</p><p>if (mysqli_connect_errno()) {</p><p>printf("Connect failed: %sn", mysqli_connect_error());</p><p>exit();</p><p>}</p><p>$query = "call yejr();";</p><p>if ($result = $mysqli->query( $query)) {</p><p>while($row = $result->fetch_row())</p><p>{</p><p>printf ("find table: %s n", $row[0]);</p><p>}</p><p>}</p><p>$result->close();</p><p>?><br></p> |
find table: yejr1
find table: yejr2
【Related articles】