Jump to content

PDO Looping


TapeGun007
Go to solution Solved by Barand,

Recommended Posts

Hey guys,

 

I just want to ask if I am doing this right because I don't want to learn bad habits.

 

To loop through a database gathering information I see a lot of "solutions" where the examples store everything into an array.  On the particular page that I am developing I don't see a need for this.  I am thinking about simply writing the code something like this:

 

 

$stmt->execute($params);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if ($row){
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
            // Display results in a table
    }
}else{
    echo "<br>No Results Found<br>";
}

 

Where a lot of examples use something more like this:

 

if ($stmt->execute()) {
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$products[] = $row;
}
}

 

I could see where storing table values into an array would make sense in some/many cases or is this a standard way of pulling database data with PDO?  Doesn't it just really depend on what you need on the page?

Link to comment
Share on other sites

Neither code snippet makes much sense. The first one completely skips the first row, which is probably not what you want. And the second one is a nonsensical reimplementation of the standard fetchAll() method.

 

If you just want to iterate over the result set, use a foreach loop, optionally with a flag to check for an empty set:

<?php

$hasProducts = false;
foreach ($productsStmt as $product)
{
    $hasProducts = true;

    // display $product
}
if (!$hasProducts)
{
    echo 'No products found.';
}

Modern template engines like Twig or Smarty have a specialized for-else statements which can do all that in one go.

Link to comment
Share on other sites

  • Solution

 

if ($stmt->execute()) {

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {

$products[] = $row;

}

}

 

The times when you might want similar to that is when you want to specify the array indexes For example

if ($stmt->execute()) {
  while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $products[ $row['user_id'] ] = $row;
  }
}

or to group results

if ($stmt->execute()) {
  while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $products[ $row['cat_id'] ][] = $row;
  }
}
  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.