2

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)
}
8
  • What does $results look like? Commented Jan 7, 2013 at 19:42
  • Please post a var_dump($results) Commented Jan 7, 2013 at 19:43
  • And please post the var_dump in your question not as a comment! Commented Jan 7, 2013 at 19:45
  • 2
    Any reason you're not using mysqli_fetch_assoc()? If you're not referencing the results by a numeric index... Commented Jan 7, 2013 at 19:51
  • And it seems to work, ["OPENAMT"]=> float(0) is in the array. Maybe the issue is somewhere else in your code? Like the part that tries to access OPENAMT after it returns. Commented Jan 7, 2013 at 19:53

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Ahh that would make perfect sense. I feel so dumb now, I have read the documentation on mysqli_fetch_arrays 100 times now and missed that everytime
OK, after you try the fix I'll be glad to know if it really solves the issue.
It definitely fixed the problem. I would upvote your answer if I could!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.