Home >Backend Development >PHP Tutorial >How to get MySQL procedure results through PHP_PHP Tutorial

How to get MySQL procedure results through PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-15 13:24:34762browse

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>

2. Use mysqli to write test code:

<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>

The results are roughly as follows:

find table: yejr1

find table: yejr2

【Related articles】


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446764.htmlTechArticleNetizens always ask me, how to call the MySQL stored procedure through php and obtain the returned result set at the same time? Indeed, MySQL's stored procedures greatly facilitate programming and improve efficiency. However, for...
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