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

  • SEARCH
  • Home
  • 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 7600545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:56:11+00:00 2026-05-30T22:56:11+00:00

I made a blog. Everything is working correctly except for one thing; being able

  • 0

I made a blog. Everything is working correctly except for one thing; being able to compare my id in order to open the appropriate article.

I’m using FETCH_ASSOC:

$sth->setFetchMode(PDO::FETCH_ASSOC); 

I’m checking $_GET for an id:

if (isset($_GET['id']) && ($_GET['id'] == $blogid['id'])) { **PROBLEM IS WITH $blogid
$row = $sth->fetch()
//... Code to display html and db values 

If true, it shows the entire blog post. Else it displays all the blog intros:

<?php } else { ?>
<?php while($row = $sth->fetch()) {  ?>
//... Code to display html and db values

How can I access the id in my if statement? If I do something like:

$blogid = $sth->fetch()

it screws up $row = $sth->fetch() by reordering the posts

EDIT Query added:

$sth = $dbh->query('SELECT id, title, slug, body, image, author, date, category from blog ORDER BY date DESC');
  • 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-30T22:56:13+00:00Added an answer on May 30, 2026 at 10:56 pm

    You’re using PDO and MySQL slightly wrong here. Instead of getting all the blog posts and checking to see whether the one you’ve got has the correct ID, you should only get one blog post (row) from the DB using a WHERE clause, like this:

    SELECT id, title, ... FROM blog 
    WHERE id=[ID from $_GET] 
    ORDER BY date DESC
    

    PDO handles this for you fantastically well by allowing you to use placeholders:

    SELECT id, title, ... FROM blog 
    WHERE id=:theBlogID 
    ORDER BY date DESC
    

    Here, I’ve used the placeholder :theBlogID which will be replaced by the $_GET parameter .

    Using PDO, your final code needs to prepare the query, bind a parameter to it, execute it and then get the result (checking if there actually is a result using PDO::rowCount()) something like this:

    $sth = $dbh->prepare('SELECT id, title, ... FROM blog 
                          WHERE id=:theBlogID 
                          ORDER BY date DESC');
    
    $sth->bindParam(':theBlogID', $_GET['id'], PDO::PARAM_INT);    // Bind the ID from $_GET to the placeholder
    
    $result = $sth->execute();    // Execute (run) the query
    
    if($result->rowCount()) {    // Have we found any blog posts with the ID specified?
        $data = $result->fetchAll();
    
        foreach($data as $post) {
            // Print out your blog post
        }
    }
    

    It’s not clear how much of the above you currently have as you only give one or two line snippets here and there, but this should give a good outline of what you need to do.

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

Sidebar

Related Questions

So I'm going to be working on a home made blog system in PHP
I'm working on a small self made blog and I insert new blog items
I have never made a blog before. I am proficient with html and css.
I recently made this wordpress blog, where you can sign up a team for
Reading this blog post about HttpOnly cookies made me start thinking, is it possible
I was reading a blog-post of Ben Scheirman about some NHibernate tweaks he made
I made a blog widget in flex. I need manipulate cookie in my widget
I have a bit of a problem. I made a blog for my webpage,
I'm creating a function to show blog's. So I made a show blog function
i'm very confused about how to add css files to a blog a made

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.