总有网友问我,如何通过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> |
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> |
结果大致如下:
find table: yejr1
find table: yejr2
【相关文章】
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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Assassin's Creed Shadows: Seashell Riddle Solution
2 weeks agoByDDD
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version
Visual web development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

Notepad++7.3.1
Easy-to-use and free code editor

Atom editor mac version download
The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.