0

I'm a begginer in PHP and I'm trying to connect my application with database using PDO.

$dbName = "new_schema";
$this->db = new PDO("mysql:host=localhost;dbname=$dbName", "root", "");

When I try to run my application, this is the output of browser:

Fatal error: in C:\xampp\htdocs\Project\application\model-database.class.php on line 10

Line 10 is this:

$this->db = new PDO("mysql:host=localhost;dbname=$dbName", "root", "");

In Workbench, Hostname is set as localhost, port 3308 and username root. No password. Connection name is Local instance mysqlweb.

In Xampp, Mysql run on port 3308.

Thanks very much for any help. :)

EDIT:

This is the browser output

Now, the error is could not find driver.

JG.
  • 9
  • 1
  • 1
  • 6
  • 2
    What is the fatal error you're getting? – JimL Jul 31 '17 at 20:02
  • Not enough information – aidinMC Jul 31 '17 at 20:07
  • MySQL default port is 3306. If you use another one, you must define it in your dns string. – Paul Spiegel Jul 31 '17 at 20:36
  • Thanks. How can I define this DNS string? – JG. Jul 31 '17 at 20:42
  • This is your [**DSN** (Data Source Name)](http://php.net/manual/en/ref.pdo-mysql.connection.php): `mysql:host=localhost;dbname=$dbName`. Add `;port=3308` to it. – Paul Spiegel Jul 31 '17 at 20:48
  • Thanks for reply. Changed, partly helped. Now, I can display a static site, but when I try to display a site with data from db, this error occurs: **could not find driver Fatal error: Uncaught Error: Call to a member function prepare() on null in C:\xampp\htdocs\Project\application\model-database.class.php:250 Stack trace: #0 C:\xampp\htdocs\Project\application\con-realised.class.php(18): ModDatabaze->getRealised() #1 C:\xampp\htdocs\Project\application\con-index.php(33): ConRealised->getResult() #2 {main} thrown in C:\xampp\htdocs\Project\application\model-database.class.php on line 250** – JG. Jul 31 '17 at 20:55
  • But I have data in this table. – JG. Jul 31 '17 at 20:56
  • This is the code: `$sql = "SELECT * FROM `realised`\"; $query = $this->db->prepare($sql);` – JG. Jul 31 '17 at 20:59

2 Answers2

1

Try the following code:

$host       = '127.0.0.1'; //or localhost
$database   = 'mysql';
$port       = 3306;
$user       = 'root';
$password   = '';

try {
    $this->connection = new PDO($database . ":host=" . $host . ';port=' . $port, $user, $password);
    $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    return $this->connection;
} catch (PDOException $e) {
    echo $e->getMessage();
}
Channaveer Hakari
  • 2,769
  • 3
  • 34
  • 45
0

Did you enable the driver in the PHP.INI file?

In your PHP.INI search for extension=php_pdo.dll and extension=php_pdo_mysql.dll and remove the ; in front of them. Save the file and restart the XAMPP-server.

Edit


I haven't had the opportunity to run it my self, not on my dev machine today, but you can try running this code in a new script to see if you get some more details on the error. I have set it up for throwing and catching errors, as well as dumping the phpinfo() page to see if PDO is enabled and MySQL drivers are showing up.

<?php

class DBTest 
{
    private $dbname;
    private $host;
    private $port;
    private $username;
    private $password;
    private $driver;

    private $db;
    
    public __construct($username, $password, $dbname = "", $host = "127.0.0.1", $port = 3306, $driver = "mysql")
    {
        $this->username = $username;
        $this->password = $password;
        $this->dbname = $dbname;
        $this->host = $host;
        $this->port = $port;
        $this->driver = $driver;
    }
    
    private function getDSN()
    {
        return sprintf("%s:host=%s;dbname=%s;port=%d", $this->driver, $this->host, $this->dbname, $this->port);
    }

    private function connect()
    {
        try
        {
            $this->db = new PDO($this->getDSN(), $this->username, $this->password);
            $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        } 
        catch (PDOException $e) 
        {
            print "Error!: " . $e->getMessage() . "<br/>";
            return false;
        }

        return true;
    }

    public function getRealised()
    {
        if($this->db == null)
        {
            return false;
        }

        $sql = "SELECT * FROM realised";

        try
        {
            $query = $this->db->prepare($sql); 
            $query->execute();
            while($row = $query->fetch(PDO::FETCH_ASSOC)) 
            {
                print_r($row);
            }
        }
        catch(PDO_Exception $e)
        {
            print "Error!: " . $e->getMessage() . "<br/>";
            return false;
        }

        return true;
    }
}

$dbtest = new DBTest("root", "", "new_schema", "127.0.0.1", 3308, "mysql");
print("Created object");

if($dbtest->connect())
{
    echo "Connected to DB";
}

if($dbtest->getRealised())
{
    echo "Database queried";
}

phpinfo();
Community
  • 1
  • 1
Canis
  • 4,130
  • 1
  • 23
  • 27
  • Thanks for help. The `extension=php_pdo_mysql.dll` was already there without `;` in front of it. I added `extension=php_pdo.dll` and restarted XAMPP. But the error **could not find driver** is still there. – JG. Jul 31 '17 at 20:22
  • Hm... Perhaps there is a problem with your paths. Check this answer to see if that helps: https://stackoverflow.com/questions/25948767/php-how-to-install-pdo-driver-windows – Canis Jul 31 '17 at 20:28
  • The path to extesion directory is correct (and is set as absolute). Both of these extensions are in this directory. – JG. Jul 31 '17 at 20:40
  • @JennyG. Added some code to try to get more info on error. – Canis Aug 03 '17 at 21:27