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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:51:45+00:00 2026-06-07T04:51:45+00:00

Here is Database.php : <?php /*Data Base Class * MySQL – InnoDB * PHP

  • 0

Here is Database.php:

<?php
/*Data Base Class
* MySQL - InnoDB 
* PHP - PDO (PHP Data Object -So we could change databases if needed)
*/
class Database extends PDO{
    private $DBH;

    function __construct($host, $dbname, $user, $pass){
        try {
            $this->DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
        } catch (PDOException $e) {  
            return $e->getMessage();  
        }
    }

    public function alteration_query($sql){
        /* Begin a transaction, turning off autocommit */
        $this->DBH->beginTransaction();
        try{
            $count = $this->DBH->exec($sql);
            $this->DBH->commit();
            return $count;
        }catch (PDOException $e) {
          $this->DBH->rollback();
          return  $e;
        }
    }
}
?>

Here is test.php:

<?php
require('Database.php');
$dbo = new Database('***.***.com','***','***','***');
echo $dbo->alteration_query('DELETE * from T_Table');
?>

For some reason, it won’t give me an error or delete the contents of the T_table.

  • 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-07T04:51:47+00:00Added an answer on June 7, 2026 at 4:51 am

    EDIT: The problem in your case is that the argument is called $sql, but you’re using $query to execute it (in the alteration_query method). Next time, please enable error reporting, and/or use a decent IDE, that can show you these errors. Like so:

    Example

    EDIT2: Set PDO’s error mode to exception, this way any error would throw an exception. See updated code.

    Don’t catch your exceptions inside of the functions, do it outside:

    <?php
    /*Data Base Class
    * MySQL - InnoDB 
    * PHP - PDO (PHP Data Object -So we could change databases if needed)
    */
    class Database
    {
        private $DBH;
    
        function __construct($host, $dbname, $user, $pass)
        {
            $this->DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
            //Set PDO to throw exceptions on errors!
            $this->DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        }
    
        public function alteration_query($sql)
        {
            /* Begin a transaction, turning off autocommit */
            $this->DBH->beginTransaction();
            $count = $this->DBH->exec($query);
            $this->DBH->commit();
            return $count;
        }
    }
    
    try {
        $pdo = new Database("localhost", "dbname", "user", "pass");
        $pdo->alteration_query("SELECT * FROM wrong_table");
    }
    catch (PDOException $e) {
        die("An error has occured! " . $e->getMessage());
    }
    

    This way, you can catch the error exactly where you need it, and not force it inside of the function (which kinda beats the point of the exception).

    Also, from the manual:

    When the script ends or when a connection is about to be closed, if
    you have an outstanding transaction, PDO will automatically roll it
    back
    . This is a safety measure to help avoid inconsistency in the
    cases where the script terminates unexpectedly–if you didn’t
    explicitly commit the transaction, then it is assumed that something
    went awry, so the rollback is performed for the safety of your data
    .

    Exceptions halt the execution of the function, meaning the commit() never happens, and it rolls back.

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

Sidebar

Related Questions

PHP/MySQLisolating database access in class - how to handle multiple row Selects Here’s a
Here is my php that retrieves image from mysql database. I want to resize
Here my code, I need to insert into mysql database I have mysql,php,android 1)
PHP mysql database I have created a follow on question to this one here
Here's the one I'm using: <?php final class Database { private static $oDb; public
Here's the code I'm trying to make work: <?php class database { var $connection;
this here below is my class database in php <?php class DB { private
class.mysql.php has 2 classes: MySQL and MySQLResult class.mysql.php: <?php /** * MySQL Database Connection
I'm new to php oop.here i wanted to do database connectivity with singleton class
I have a database class that uses PDO. Here's a portion example of it:

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.