1

I am trying to print my result data returned from the fetchAll function. How can i bind the name and desc variable to the returned results.

 <?php
$get = $db->prepare("SELECT * FROM Products ORDER BY DateAdded DESC LIMIT 4");
$get->execute(); 
$results = $get ->fetchAll(); 

  foreach($results as $result){ ?>
              $name = $result['Name'];
              $desc= $result['Desc'];
           <h4><?php echo $name ?></h4>
           <h4><?php echo $desc?></h4>

         <?php
         }
  ?>
1
  • 2
    ?> should be after $desc= $result['Desc'];. Make an effort, learn php syntax carefully. Commented Jan 8, 2017 at 19:31

2 Answers 2

1

Try the following:

$results = $get->fetchAll(PDO::FETCH_ASSOC); 

foreach($results as $key => $value){ ?>
   echo '<h4>'.$value['name'].'</h4>
       <h4>'.$value['desc'].'</h4>';         
}

You also had a space between $get and ->fetchAll()

You might want to remove the php closing tag ?> as it might give you problems when including.

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

Comments

0

You don't have to fetch all results at once to do that. It's better to use fetch method for that task.

while ($row = $get->fetch(PDO::FETCH_ASSOC)) {
    echo $row['Name'];
}

And about main problem here, you are missing closing tag after $result['Desc'];

Comments

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.