search
HomeBackend DevelopmentPHP Tutorial为什么php调用存储过程返回多个结果集,总要多出一个结果集

比如
$db->query("SET NAMES UTF8");   
if($db->real_query("call sp_test()")){   
    do{   
        echo 'bp';
        if($result = $db->store_result()){   
            while ($row = $result->fetch_assoc()){   
                array_push($rows, $row);   
            }   
            $result->close();   
        }   
    }while($db->next_result());   
}   
$db->close(); 

你会发现假设你返回1个结果集,会打出2个bp,2个结果集就会echo出3个bp,但如果不写存储,直接写语句,就正常,不会多出一个,求大神指点迷津!


回复讨论(解决方案)

这不是很正常吗?不然你要 if($result = $db->store_result()){ 干什么?

这不是很正常吗?不然你要 if($result = $db->store_result()){ 干什么?

您好,可是直接写语句在php里不会多出一个,比如select 1;select 2;

他就打出2个bp,这是为什么呢,存储过程里也写以上2句就会打出3个

php 的数据库函数不支持形如 select 1;select 2; 这样的查询,所以不能作为参照系

   do{           if($result = $db->store_result()){           }       }while($db->next_result());   
这样的代码结构是读取存储过程结果集必须的,如果你认为是 bug,可以去 php bug 网站去投诉。
不过在没有改正之前,你还是必须这么做

php 的数据库函数不支持形如 select 1;select 2; 这样的查询,所以不能作为参照系

   do{           if($result = $db->store_result()){           }       }while($db->next_result());   
这样的代码结构是读取存储过程结果集必须的,如果你认为是 bug,可以去 php bug 网站去投诉。
不过在没有改正之前,你还是必须这么做

非常感谢

因为你echo写在了 if($result = $db->store_result()){ 的前面,而有没有结果是要看 if($result = $db->store_result()){ 的判断结果才知道,所以肯定有2个结果的时候, if要判断3次,也就是你说的3次bp

其实简单说,你的困惑在于

$i = 1;$max = 2;while(true) {	echo "A"; // 这里肯定会输出3个A	if($i > $max) {	    break;	}	$i++;	}

php 的数据库函数不支持形如 select 1;select 2; 这样的查询,所以不能作为参照系

   do{           if($result = $db->store_result()){           }       }while($db->next_result());   
这样的代码结构是读取存储过程结果集必须的,如果你认为是 bug,可以去 php bug 网站去投诉。
不过在没有改正之前,你还是必须这么做

大侠,不好意思,我还想问下,$result = $db->store_result(),这句话到底是什么意思,这是赋值运算,怎么就能返回bool值呢?

$db->store_result() 返回一个查询结果资源
当没有了的时候,返回的就是空了(null)

$db->store_result() 返回一个查询结果资源
当没有了的时候,返回的就是空了(null)

那$result =null  这个返回的是false吗?

还真是false,一个赋值表达式都能返回false,php真神奇

是我理解错了,运来if里面判断的仅仅是$result的值!!

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
Working with Flash Session Data in LaravelWorking with Flash Session Data in LaravelMar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

cURL in PHP: How to Use the PHP cURL Extension in REST APIscURL in PHP: How to Use the PHP cURL Extension in REST APIsMar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Simplified HTTP Response Mocking in Laravel TestsSimplified HTTP Response Mocking in Laravel TestsMar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

12 Best PHP Chat Scripts on CodeCanyon12 Best PHP Chat Scripts on CodeCanyonMar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

How to Register and Use Laravel Service ProvidersHow to Register and Use Laravel Service ProvidersMar 07, 2025 am 01:18 AM

Laravel's service container and service providers are fundamental to its architecture. This article explores service containers, details service provider creation, registration, and demonstrates practical usage with examples. We'll begin with an ove

PHP Logging: Best Practices for PHP Log AnalysisPHP Logging: Best Practices for PHP Log AnalysisMar 10, 2025 pm 02:32 PM

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

Explain the concept of late static binding in PHP.Explain the concept of late static binding in PHP.Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Customizing/Extending Frameworks: How to add custom functionality.Customizing/Extending Frameworks: How to add custom functionality.Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools