简体   繁体   中英

Converting from php mysql to mysqli mysqli_fetch_array

I am in the middle of converting a program from the mysql library to mysqli.

Edit: Some more context

I have the following code calling a class

case "purchasing";
    $tab = 'purchasing';
    require $classes . 'purchasing.php';
    $purchases = new Purchase_list($search_term, $scope);
    require $templates . 'header.php';
    require $templates . 'purchasing.php';
    break;

The class that contains get_result below is Purchase_list Purchase list basically figures out what rows to retrieve from the database, runs a query and assigns the results to $this->result

That part is obviously working since I have a valid result. Then $templates . 'purchasing.php'; displays those results.

For trouble shooting purposes purchasing.php only contains

while ($row = $purchases->get_result()) {

}

I have a function inside a class that looks like this

function get_result() {

    $results = mysqli_fetch_array($this->result);        
    if($results === FALSE) {
        return false;
    }else {
        $results['OPENAMT'] = ($results['Puramt'] - $results['Recamt']);

        return $results;
    }


}

if I comment out the the $results['OPENAMT'] code everything works great, with that code not commented it times out the browser request. This code worked with the mysql extension instead of mysqli

Can anyone shed any light on whats actually going on here.

a var_dump on $results before trying to return gives

array(11) {
  [0]=> string(5) "23074"
  ["Purno"]=> string(5) "23074"
  [1]=> string(3) "AEC"
  ["Vendno"]=> string(3) "AEC"
  [2]=> string(10) "11/28/2012"
  ["Purdate"]=> string(10) "11/28/2012"
  [3]=> string(4) "0.00"
  ["Puramt"]=> string(4) "0.00"
  [4]=> string(4) "0.00" 
  ["Recamt"]=> string(4) "0.00"
  ["OPENAMT"]=> float(0)
}

I don't exactly know why you get this problem, but I hope this may help.

Whereas mysql_fetch_array returns FALSE when there are no more rows to fetch, mysqli_fetch_arrays returns NULL .

So $results is never === FALSE , and you always get into the else block and probably return some rubbish with warnings being fired.

As a consequence, the while loop calling get_result() never ends because instead of getting false it gets I don't know what .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM