Jump to content
Kenny_Luck

How do i print out the data form pdo

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

Share this post


Link to post
Share on other sites

Espacially this: https://phpdelusions.net/pdo#fetch as it looks like you have mostly anything else. Also have a look at print_r(), var_dump(), htmlspecialchars(), Template Engines and XSS (cross site scripting).

Edited by chhorn

Share this post


Link to post
Share on other sites

Not just fetch(), there are multiple options open, for example

  • fetch()
  • fetchAll()
  • foreach ($stmt ...)

Share this post


Link to post
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.

 

Share this post


Link to post
Share on other sites

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.