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 8647959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:12:04+00:00 2026-06-12T13:12:04+00:00

Apologies in advance because I’m really unsure how to ask this question so if

  • 0

Apologies in advance because I’m really unsure how to ask this question so if you need to know anything then please comment rather than downvote and I will edit.

I have teaser links on my main page which when clicked open up a window with the full article. I’m currently converting my MySQL code over to PDO and have gotten a little stuck.

In MySQL I used to be doing the following (Here, $foo_query is the query from the first page):

$id = $_GET['id'];

$sql = "SELECT id, postdate, title, body FROM FooBarTable WHERE id = $id";
if ($foo_query = mysql_query($sql)) {
    $r     = mysql_fetch_assoc($foo_query);
    $title = $r["title"];
    $body  = $r["body"];
}

Which is simple to understand to me. I’ve been trying to convert this using what I know, and it turns out I don’t know very much. So far I have the following:

$id = $_GET['id'];

$sql = $DBH->prepare("SELECT id, postdate, title, body FROM FooBarTable WHERE id = :id OR id = $id");
$sql->bindParam(':id', $_REQUEST['id'], PDO::PARAM_INT);
if ($foo_query = $DBH->query($sql)) {
    $r->setFetchMode(PDO::FETCH_ASSOC);
    $r     = $foo_query->fetch();
    $title = $r["title"];
    $body  = $r["body"];
}
$sql->execute();

This brings up an error of ‘PDO::query() expects parameter 1 to be string’. This is for the ‘if’ line.

Have I even written any of that PDO correctly? What would I need to do from here? A friend has recently taught me MySQL, but he doesn’t know PDO at all which means I can’t ask his advice (not all that helpful…)

  • 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-06-12T13:12:06+00:00Added an answer on June 12, 2026 at 1:12 pm

    This is the correct way, with comments:

    try {
        //Connect to the database, store the connection as a PDO object into $db.
        $db = new PDO("mysql:host=localhost;dbname=database", "user", "password");
    
        //PDO will throw PDOExceptions on errors, this means you don't need to explicitely check for errors.
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        //PDO will not emulate prepared statements. This solves some edge cases, and relives work from the PDO object.
        $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    
        //Prepare the statement.
        $statement = $db->prepare("SELECT id, postdate, title, body FROM FooBarTable WHERE id = :id");
        //Bind the Value, binding parameters should be used when the same query is run repeatedly with different parameters.
        $statement->bindValue(":id", $_GET['id'], PDO::PARAM_INT);
        //Execute the query
        $statement->execute();
    
        //Fetch all of the results.
        $result = $statement->fetchAll(PDO::FETCH_ASSOC);
        //$result now contains the entire resultset from the query.
    }
    //In the case an error occurs, a PDOException will be thrown. We catch it here.
    catch (PDOException $e) {
        echo "An error has occurred: " . $e->getMessage();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Apologies in advance, because I suspect this may be a silly question. I have
All, Apologies in advance - this question might be too open-ended for SO. Anyway...
This may be a bit of an abstract question, so apologies in advance. I
This, I'm sure is a pretty basic question about JavaScript, so apologies in advance.
This could potentially be a dumb question so apologies in advance if it is.
I apologize in advance because this is somehow a silly question, but I just
I apologize in advance if my question is not clear, because I don't know
Apologies in advance for the length of this question! I have a data structure
This is my first steps into async/threading so apologies in advance. I need some
I'd like to apologize in advance, because this is not a very good question.

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.