Table of Contents
回复内容:
mysqli的结果集是一个对象,使用is_resource函数当然是false,没什么好纠结的
Home Backend Development PHP Tutorial 关于is_resource()方法的问题问题

关于is_resource()方法的问题问题

Jun 06, 2016 pm 08:22 PM
php

在将php5.3的程序向php7迁移过程中 出现这样的问题:
1、我将mysql全换成mysqli
2、在判断mysql结果集时正确,能够正常得到结果,但是换成mysqli后也能够得到结果
现在问题原因是
换成mysqli后,is_resource()方法判断mysqli结果集的结果为false

上源代码 代码出自某商城程序

1

2

3

4

5

6

7

8

9

10

11

<code>static function sqlOfUpdate(&amp;$rs, $data, $InsertIfNoResult = false,$insertData=null,$ignore=false){

        $db = kernel::database();

        var_dump($rs,is_resource($rs['rs']));exit;

        if(!is_resource($rs['rs'])){

            trigger_error('SqlOfUpdate: '.$rs['sql'].' error ',E_USER_ERROR);

        }

        @mysqli_data_seek($rs['rs'],0);

        $row = mysqli_fetch_assoc($rs['rs']);

        if($InsertIfNoResult &amp;&amp; !$row){

            return self::getinsertsql($rs,$data);

        }</code>

Copy after login
Copy after login

输出的结果

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

<code>array(2) {//$rs

  ["rs"]=&gt;

  object(mysqli_result)#74 (5) {

    ["current_field"]=&gt;

    int(0)

    ["field_count"]=&gt;

    int(4)

    ["lengths"]=&gt;

    NULL

    ["num_rows"]=&gt;

    int(0)

    ["type"]=&gt;

    int(0)

  }

  ["sql"]=&gt;

  string(115) "select * from `sdb_base_cache_expires` where 1 AND `type` = "DB" AND `app` = "base" AND `name` = "BASE_APP_CONTENT""

}

bool(false)//is_resource($rs['rs'])</code>

Copy after login
Copy after login

刚刚查了下php.net的说明发现resource类型中存在mysql结果类型而不存在mysqli结果类型

回复内容:

在将php5.3的程序向php7迁移过程中 出现这样的问题:
1、我将mysql全换成mysqli
2、在判断mysql结果集时正确,能够正常得到结果,但是换成mysqli后也能够得到结果
现在问题原因是
换成mysqli后,is_resource()方法判断mysqli结果集的结果为false

上源代码 代码出自某商城程序

1

2

3

4

5

6

7

8

9

10

11

<code>static function sqlOfUpdate(&amp;$rs, $data, $InsertIfNoResult = false,$insertData=null,$ignore=false){

        $db = kernel::database();

        var_dump($rs,is_resource($rs['rs']));exit;

        if(!is_resource($rs['rs'])){

            trigger_error('SqlOfUpdate: '.$rs['sql'].' error ',E_USER_ERROR);

        }

        @mysqli_data_seek($rs['rs'],0);

        $row = mysqli_fetch_assoc($rs['rs']);

        if($InsertIfNoResult &amp;&amp; !$row){

            return self::getinsertsql($rs,$data);

        }</code>

Copy after login
Copy after login

输出的结果

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

<code>array(2) {//$rs

  ["rs"]=&gt;

  object(mysqli_result)#74 (5) {

    ["current_field"]=&gt;

    int(0)

    ["field_count"]=&gt;

    int(4)

    ["lengths"]=&gt;

    NULL

    ["num_rows"]=&gt;

    int(0)

    ["type"]=&gt;

    int(0)

  }

  ["sql"]=&gt;

  string(115) "select * from `sdb_base_cache_expires` where 1 AND `type` = "DB" AND `app` = "base" AND `name` = "BASE_APP_CONTENT""

}

bool(false)//is_resource($rs['rs'])</code>

Copy after login
Copy after login

刚刚查了下php.net的说明发现resource类型中存在mysql结果类型而不存在mysqli结果类型

mysqli的结果集是一个对象,使用is_resource函数当然是false,没什么好纠结的

如果要判断结果集是否存在数据可以使用mysqli结果集对象里的num_rows属性:

1

2

<code>$mysqli_result = $mysqli-&gt;query($sql);

var_dump($mysqli_result-&gt;num_rows);</code>

Copy after login

原因 @thou95 已经说了,替换方法可以这么写

1

2

static function sqlOfUpdate(mysqli_result &$rs, $data, $InsertIfNoResult = false,$insertData=null,$ignore=false){

...

Copy after login

在rs参数定义的时候加上mysqli_result类型就行了

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles