Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDO Connection to DB issues

Tags:

php

mysql

pdo

I need to know if PDO extension I wrote is valid both syntactically and semantically. I've been var_dumping() my connection variables and while the variables are being passed to the constructor (with correct values), I'm not able to actually fetch anything from my database.

I've researched the PDO class on the PHP manual and from what I've uncovered the class I'm using is nearly identical to the extension class given in the examples section of the wiki page.

Here's my code:

class DBConnector extends PDO
    {
        private $host;
        private $username;
        private $password;
        private $db;
        private $dns;

        public function __construct($host, $username, $password, $db)
        {
            $this->host = $host;
            $this->username = $username;
            $this->password = $password;
            $this->db = $db;

            $this->dns = "mysql:dbname=".$this->db.";host=".$host;
            $connection = parent::__construct($this->dns, $this->username, $this->password);

        }
    }

And here's a test query which returns an array with...nothing inside of it. There is data within the database, so obviously something isn't correct.

function testQuery()
{
    global $connection;

    $query = "
        SELECT * FROM users
    ";

    $stmt = $connection->prepare($query);

    $result = $stmt->fetchAll();


}

Am I doing something wrong?

like image 539
zeboidlund Avatar asked Sep 18 '11 22:09

zeboidlund


People also ask

How does PDO connect to database?

A PDO database connection requires you to create a new PDO object with a Data Source Name (DSN), Username, and Password. The DSN defines the type of database, the name of the database, and any other information related to the database if required. These are the variables and values we stated inside the dbconfig.

Which type of databases can PDO connect to?

MySQLi procedural and MySQLi object-oriented only support MySQL database but PDO is an advanced method along with MySQL which supports Postgres, SQLite, Oracle, and MS SQL Server. PDO is more secure than the first two options and it is also faster in comparison with MySQLi procedural and MySQLi object-oriented.

How can I get error in PDO?

You must call PDOStatement::errorInfo() to return the error information for an operation performed on a particular statement handle.

What does PDO exception mean?

PHP Data Objects (or PDO ) are a collection of APIs and interfaces that attempt to streamline and consolidate the various ways databases can be accessed and manipulated into a singular package. Thus, the PDOException is thrown anytime something goes wrong while using the PDO class, or related extensions.


2 Answers

Try this:

class DBConnector extends PDO
{
  private $connection;
  private $host;
  private $username;
  private $password;
  private $db;
  private $dns;

  public function __construct($host, $username, $password, $db)
  {
      $this->host = $host;
      $this->username = $username;
      $this->password = $password;
      $this->db = $db;
      $this->dns = "mysql:dbname=".$this->db.";host=".$host;
      $this->connection = parent::__construct($this->dns, $this->username, $this->password);
      $this->setAttribute (PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  }

  public function testQuery()
  {
      $query = "SELECT * FROM a";
      $stmt = $this->prepare($query);
      if($stmt->execute()){
          return $stmt->fetchAll();
      }
      return array();
  }
}

$tg = new DBConnector('localhost', 'root', '', 'test');
$t = $tg->testQuery();
print_r($t);

the $connection is local to the DBConnector::__construct and I don't see any global there. So it will not exist in your testQuery function. By moving your function to the class, and create a connection property, it is easy to use it.

like image 104
Book Of Zeus Avatar answered Oct 16 '22 17:10

Book Of Zeus


You need to execute query.

function testQuery()
{
    global $connection;

    $query = "
        SELECT * FROM users
    ";

    $stmt = $connection->prepare($query);

    if($stmt->execute()){

       $result = $stmt->fetchAll();

    }
}
like image 23
NiematojakTomasz Avatar answered Oct 16 '22 17:10

NiematojakTomasz