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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:30:47+00:00 2026-06-12T17:30:47+00:00

I am new to PDO and a few things about it confuse me, I’ve

  • 0

I am new to PDO and a few things about it confuse me, I’ve tried creating a test function to see whether an exception will be thrown for an invalid query but nothing is thrown.

here is the code

<?php
include_once("/var/www/include/constants.php");

class DB{
    private $DBH; 

    public function DB(){
        try{
            $DBH = new PDO("mysql:host=".DB_SERVER.";dbname=".DB_NAME, DB_USER, DB_PASS);   
        }
        catch(PDOException $e) {  
            echo $e->getMessage(); 
        }
    }

    public function test(){
        try{
            $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );  
            $DBH->prepare('DELECT id FROM users');  
        }
        catch(PDOException $e) {  
            echo $e->getMessage();  

        }
    }

};

/* Create database connection */
$db = new DB;
$db->test();

?>
  • 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-12T17:30:48+00:00Added an answer on June 12, 2026 at 5:30 pm

    Besides the missing references to the $this of your database handle, your need to tell PDO that it must not emulate prepares. The code below will throw a exception like this:

    SQLSTATE[42000]: Syntax error or access violation: 1064 You have an
    error in your SQL syntax; check the manual that corresponds to your
    MySQL server version for the right syntax to use near ‘DELECT id FROM
    users’ at line 1

    class DB{
        private $DBH; 
    
        public function DB(){
            try{
                $this->DBH = new PDO("mysql:host=localhost;dbname=movies", 'root', 'jsat12');   
                $this->DBH->setAttribute(PDO::ATTR_EMULATE_PREPARES, false );
          }
            catch(PDOException $e) {  
                echo $e->getMessage(); 
            }
        }
    
        public function test(){
            try{
                $this->DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );  
                $this->DBH->prepare('DELECT id FROM users');  
            }
            catch(PDOException $e) {  
                echo $e->getMessage();  
    
            }
        }
    
    };
    
    /* Create database connection */
    $db = new DB;
    $db->test();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

New to PHP and MySQL, have heard amazing things about this website from Leo
Hello i was trying to make function with PDO but getting error (new to
I'm new to pdo just tried the following and getting fatal error. $pdo =
I have the following code: public static function check($ip,$op,$page) { $con = new PDO(DBN,DB_USER,DB_PASS);
New to PDO. Here is my function. We are connected to the database and
$file_db = new PDO('sqlite:test.db'); if($file_db){ $result = $file_db->query('SELECT id FROM sample'); $encodable = array();
I am trying to get the new PDO driver running in Code Igniter 2.1.1
while using the following constructor $this->pdo = new PDO ($this->source, $this->username, $this->password); if I
Why doesn't this PHP PDO snippet work? $DBH = new PDO(mysql:host=$host;dbname=$dbname, $user, $pass); $STH->bindParam(':fftb',
What is the difference between: $db = new SQLiteDatabase('name.db'); and $db = new PDO('sqlite:name.db');

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.