1

I am having difficult loop through the data in PDO and print the data as long as there it has more data for a specific user. I create a function which performs the select. Here is the code that contain the select function, http://pastebin.com/GiAyCBys. I am trying to use that function in cartexe.php using the following code,

    while($row = select($conn, 'user', 'cart', $user,':user','*'))
   {    
       echo 'Hello';
    }

but I got stuck in an infinite loop. I am grateful for any help I can get.

6
  • I still get stuck in an infinite loop. Commented Oct 1, 2014 at 21:46
  • This might help you as it poses a similar question: stackoverflow.com/questions/3601534/infinite-php-while-loop Commented Oct 1, 2014 at 21:47
  • I can definitely do while using mysqli or mysql query without any difficult. This is a bit different because I am using PDO and I am also calling function. Commented Oct 1, 2014 at 21:49
  • Then I think your only recourse is to set a condition to allow you to exit the loop or do not use a while loop. Commented Oct 1, 2014 at 21:51
  • 1
    Then answer your own question and show us how you came to your conclusion. Might help out another poor soul with the same issue. Commented Oct 1, 2014 at 22:05

1 Answer 1

1

The solution is to change select to return all rows , since fetch() only return a single row at the time.

Option 1:

$result = array();
while($row = $smtp->fetch(PDO:: FETCH_ASSOC)){    
  $result[]=$row;
}             
return $result;

option 2:

$result = $smtp->fetchAll(PDO:: FETCH_ASSOC);
return $result;

use your function like this

$rows = select($conn, 'user', 'cart', $user,':user','*');
foreach($rows as $row){
 //do something with $row
}
Sign up to request clarification or add additional context in comments.

1 Comment

The fetch and for each was how I solved the problem like hours ago. I really appreciate the inputs as they were what gave me a better insight on approaching, tackled and corrected the problem.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.