Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7532621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:30:27+00:00 2026-05-30T05:30:27+00:00

So I’ve finally decided to update my PHP for the year 2012 by learning

  • 0

So I’ve finally decided to update my PHP for the year 2012 by learning to use PHP’s PDO. So far everything is going great, however I don’t know if the way I’m going about it is really the best way of doing it.

In this example I am querying my database to display posts users have made, and then displaying 2 comments for each post. So basically what I’m doing is grabbing my posts, looping it out, then in that loop I query the database for the top two comments for every post. However before I run around and start using this all year, I figured I’d see if there is a cleaner method of doing this.

So if anyone could spare a moment to look over this small block of code and let me know if there is a cleaner and perhaps more efficient way of doing this I’d really appreciate it. Feel free to nitpick!

<?php
$hostname = 'localhost';
$username = 'root';
$password = 'root';
$database = 'database';
try {
    $dbh = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

//Get Posts
    $stmt = $dbh->prepare("SELECT * FROM posts");
    $stmt->execute();
    $result = $stmt->fetchAll();
    }
catch(PDOException $e)
    {
    echo $e->getMessage();
    }

//Loop through each post
foreach($result as $row) {

echo $row['post'];

//Get comments for this post
    $pid = $row['id'];
    $stmt = $dbh->prepare("SELECT * FROM comments WHERE pid = :pid LIMIT 2");
    $stmt->bindParam(':pid', $pid, PDO::PARAM_STR);
    $stmt->execute();
    $c_result = $stmt->fetchAll();

//Loop through comments
foreach($c_result as $com) {

echo $com['comment'];

    }
}
//Close connection
$dbh = null;
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T05:30:29+00:00Added an answer on May 30, 2026 at 5:30 am

    Well, it is actually 2 questions.

    .1. For the code you are using, it is quite ugly. Using raw API functions always makes your code ugly, boring and repetitive. To run just one query took you FIVE lines!
    Don’t you think that just one line would be better? Line consists of only meaningful operators?

    $comments = $db->getAll("SELECT * FROM comments WHERE pid = :pid LIMIT 2",$row['id']);
    

    .2. For the algorithm – it is quite okay.
    Assuming you are *not going to loop over all your database, but merely request only 10-20 posts per page, additional 10-20 primary-key based lookups won’t slow your application much.

    .3. Bonus track.
    thing you really may want to consider is a “business logic/presentation logic separation”. Why not to get all your data first and only than starting an output? it will make your code way more clean.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
I am trying to understand how to use SyndicationItem to display feed which is
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.