Question about the is_resource () method

Source: Internet
Author: User
This problem occurs during the migration from a php5.3 program to php7: 1. I replace all mysql with mysqli2. The mysql result set is correct and the result can be obtained normally, however, after changing to mysqli, you can also get the result. The cause is that after changing to mysqli, The is_resource () method determines... this problem occurs during the migration of php5.3 programs to php7:
1. I replace mysql with mysqli.
2. If the mysql result set is correct, the result can be obtained normally, but the result can also be obtained after mysqli is replaced.
The cause is
After mysqli is changed, the is_resource () method determines that the result set of mysqli is false.

The source code is from a mall program.

static function sqlOfUpdate(&$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 && !$row){            return self::getinsertsql($rs,$data);        }

Output result

array(2) {//$rs  ["rs"]=>  object(mysqli_result)#74 (5) {    ["current_field"]=>    int(0)    ["field_count"]=>    int(4)    ["lengths"]=>    NULL    ["num_rows"]=>    int(0)    ["type"]=>    int(0)  }  ["sql"]=>  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'])

After checking the description of php.net, we found that mysql result type exists in the resource type, but mysqli result type does not exist.

Reply content:

This problem occurs during the migration of php5.3 programs to php7:
1. I replace mysql with mysqli.
2. If the mysql result set is correct, the result can be obtained normally, but the result can also be obtained after mysqli is replaced.
The cause is
After mysqli is changed, the is_resource () method determines that the result set of mysqli is false.

The source code is from a mall program.

static function sqlOfUpdate(&$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 && !$row){            return self::getinsertsql($rs,$data);        }

Output result

array(2) {//$rs  ["rs"]=>  object(mysqli_result)#74 (5) {    ["current_field"]=>    int(0)    ["field_count"]=>    int(4)    ["lengths"]=>    NULL    ["num_rows"]=>    int(0)    ["type"]=>    int(0)  }  ["sql"]=>  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'])

After checking the description of php.net, we found that mysql result type exists in the resource type, but mysqli result type does not exist.

The result set of mysqli is an object. The use of the is_resource function is certainly false, so there is nothing to worry about.

To determine whether data exists in the result set, you can use the num_rows attribute in the mysqli result set object:

$mysqli_result = $mysqli->query($sql);var_dump($mysqli_result->num_rows);

The reason @ thou95 has already been mentioned. The replacement method can be written like this.

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

When defining the rs parameter, addmysqli_resultType.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.