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.

TheAmazingKnight
  • 2,442
  • 9
  • 49
  • 77

1 Answers1

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
}
meda
  • 45,103
  • 14
  • 92
  • 122
  • 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. – TheAmazingKnight Oct 02 '14 at 01:24