Jump to content

How do i print out the data form pdo


Kenny_Luck

Recommended Posts

so here the code that i am confusing the whole month , how can i get it to print out the filtered data

$sql="SELECT COUNT(*) as conflicts
        FROM rental
        WHERE Dispatch < :dropoff AND Dropoff > :dispatch";
$stmt = $pdo->prepare($sql);
$stmt->execute( [ 
                   'dropoff'  => $Dropoff,
                   'dispatch' => $Dispatch
                ]);

im so frustrated on myself that cant do anything

Link to comment
Share on other sites

While reading the item linked to by Barand would be a great place to improve your PDO knowledge, a simple loop on a fetch is the usual way of outputting the results of a query.  One problem with your query though is all you are asking for is a count of the rows that match your where condition.  Don't you want so select some fields from those matching records to be displayed?

After that you simply do this:

echo "<table>";
while ($row = $stmt-fetch(PDO::FETCH_ASSOC))
{
  echo "<tr>
  <td>{$row['fieldname1']}</td>
  <td>{$row['fieldname2']}</td>
  <td>.....</td>....
  </tr>";
}
echo "</table>";

This will give you a nice html table of the results.  You can add your own table headings (th tags) to improve it.  And you could add some css code to make more improvements.

Note - the "fieldname1 and "fieldname2" are made-up names for your query results.  Obviously you would substitute your queried item names for the $row elements.  

PS - you should also add some error handling code to ensure that the query has actually run or that you have some results to be displayed before starting the loop.

 

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.