Home > php教程 > php手册 > 如何通过PHP取得MySQL procedure结果

如何通过PHP取得MySQL procedure结果

WBOY
Release: 2016-06-13 11:01:37
Original
1470 people have browsed it

总有网友问我,如何通过php调用MySQL的存储过程同时取得返回的结果集呢?确实,MySQL的存储过程大大方便了编程也提高了效率。但是,对于那些还在用php 4 的同学们来说可就麻烦了,因为php 4只能调用存储过程,但却无法直接取得返回结果集;不过,用php 5的mysqli函数就可以做到了。首先,重新编译php 5,增加对mysqli的支持,或者直接下载mysqli的扩展模块,这里不再细说。直接举个例子吧:

1、创建存储过程,列出 test 库下的所有表:

<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>
Copy after login
 
2、用 mysqli 编写测试代码:

<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>
Copy after login
 
结果大致如下:

find table: yejr1

find table: yejr2

【相关文章】


source:php.cn
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template