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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:37:39+00:00 2026-06-06T04:37:39+00:00

I am looking for a PHP PDO full working example with best practices for

  • 0

I am looking for a PHP PDO full working example with best practices for running a query and handling errors. Here’s what I have so far.

CONNECTING. If you don’t do it this way, a connection failure will by default expose DB credentials to all users of your site.

try {
    $dbh = new PDO("mysql:host=localhost;dbname=phor_lang", "phor_lang", "'9lsnthsn9");
} catch (PDOException $e) {
    error(false, "PDO ERROR: " . $e->getMessage());
}

QUERYING

$stmt = $dbh->prepare("INSERT INTO sets");
$stmt->execute()
    or error(0, "USERS ERROR ".__LINE__." ".print_r($dbh->errorInfo(),true));
$setID = $dbh->lastInsertID();
$stmt->closeCursor();

$stmt = $dbh->prepare("INSERT INTO words (language, name, detail, user, type, set) VALUES (?, ?, ?, ?, ?, ?)");
$stmt->execute(array($l1l, $l1w, $l1d, $userID, 'training', $setID))
    or error(0, "USERS ERROR ".__LINE__." ".print_r($dbh->errorInfo(),true));
$stmt->closeCursor();

However, this is resulting in queries that fail (execute returns false) and the error message is blank.

  • 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-06T04:37:41+00:00Added an answer on June 6, 2026 at 4:37 am

    Here‘s a modern starter’s guide to PDO. It answers some of your questions and explains a lot of other basic PDO functionality.

    I just read over it yesterday and found it an excellent resource!

    Here’s a quote under ‘errors’:

    try {
        $conn = new PDO('mysql:host=localhost;dbname=myDatabase', $username, $password);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch(PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using PDO with prepared statements (I learned from here: http://www.kitebird.com/articles/php-pdo.html ) I'm using a
I have a class that uses PHP's ereg() which is deprecated. Looking on PHP.net
I'm having a strange problem with php PDO and mysql. I have the following
I've been looking at how best to protect against sql injection in PHP/mysql beyond
So far everytime i use to make a select query using pdo, i used
I have been looking for help online on how to design my php classes
Looking for php help, best practice. What is a good way to break these
In PHP with PDO, what characters are we limited to using. I've tried looking
I have been looking around the current options (and related SO questions) regarding PHP
I'm looking to php preg_match to break out a faceted search string. example search

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.