Jump to content

pdo while loop


Recommended Posts

i try to do while loop using fetchAll...but only 1st item will be the output. thanks for the help

  function cart_display(){
 global $db;
  		$query = $db->prepare(" SELECT * FROM cart where ip_add = '$ip'");
	  while ($row=$query->fetch()):
		$query = $db->prepare("SELECT * FROM product where product_id = :product_id"); 
	   $query->bindParam(':product_id', $product_id);
	    $rows = $query->fetchAll(PDO::FETCH_ASSOC);

	  echo" <tr>
	  <td><input type = 'checkbox' name='remove[]' value='<?php echo $product_id?>'/> </td>
	  <td>".$row['product_name']." <br><img src='admin/product_images/".$row['product_image']."' width='80' height='80'/></td>
		<td><input type='text' name='qty' value='".$row['qty']."'/></td>
Link to comment
Share on other sites

There's a variable inside the prepared statement, you should never put variables directly in the statement, use a placeholder and pass the variable in using bindParam.


You're overwriting variables $query and $row from outside the loop, which will cause it to stop looping through rows of the first query.


When calling fetchAll(), you get an array that you have to loop through, but in your code you're not looping through that array, instead you called the fetch() method which gets only the first row.


Prepared statements only have to be prepared once, preparing it inside the loop is inefficient.


I've fixed your code, you should use more descriptive variable names so that you don't overwrite them.

function cart_display() {
  global $db;

  // Get IP address
  $ip = getIp();

  // Prepare the cart statement
  $cart_query = $db->prepare('SELECT p_id FROM cart WHERE ip_add = :ip');
  $cart_query->bindParam(':ip', $ip);

  // Execute the statement

  // Prepare the next statement to be used in the loop
  $product_id = ''; // Initialize variable
  $product_query = $db->prepare("SELECT * FROM product where product_id = :product_id");
  $product_query->bindParam(':product_id', $product_id);

  // Loop through results of the first query and execute the second query
  while($cart = $cart_query->fetch(PDO::FETCH_ASSOC)) {

    // Thanks to the magic of bound parameters
    // $product_id is automatically passed into the query
    $product_id = $cart['p_id'];

    // Loop through products
    while($product = $product_query->fetch(PDO::FETCH_ASSOC)) {
      // Print a table row with product values
      echo "
          <td><input type = 'checkbox' name='remove[]' value='{$product_id}'/> </td>
          <td>{$product['product_name']} <br><img src='admin/product_images/{$product['product_image']}' width='80' height='80'/></td>
          <td><input type='text' name='qty' value='{$row['qty']}'/></td>
  • Like 1
Link to comment
Share on other sites

in this part $product->execute();- i got this error. Notice: Undefined variable: product in C:\xampp\htdocs\hcc\cart.php on line 375

Fatal error: Uncaught Error: Call to a member function execute() on null in C:\xampp\htdocs\hcc\cart.php:375 Stack trace: #0 C:\xampp\htdocs\hcc\cart.php(306): cart_display() #1 {main} thrown in C:\xampp\htdocs\hcc\cart.php on line 375

Link to comment
Share on other sites

I don't usually test code when I write it for the forum.


My hope is that you'll understand the theory behind it and learn from it. If you understood it correctly you should be able to fix any mistakes I made.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...