简体   繁体   中英

PDO: database credentials

I am trying to make my own Database class. Although I have declared all credentials the errors tell me that there is no database specified.

Fatal error: Uncaught PDOException: SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected in /var/www/html/jwagter/work/database/Database.php:69 Stack trace: #0 /var/www/html/jwagter/work/database/Database.php(69): PDOStatement->execute() #1 /var/www/html/jwagter/work/database/tutorial.php(22): Database->execute() #2 {main} thrown in /var/www/html/jwagter/work/database/Database.php on line 69

index.php:

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

define("DB_HOST", "localhost");
define("DB_USER", "root");
define("DB_PASS", "secret");
define("DB_NAME", "test");

require 'Database.php';


$database = new Database();

$database->query('INSERT INTO mytable (FName, LName, Age, Gender) VALUES (:fname, :lname, :age, :gender)');

$database->bind(':fname', 'Joris');
$database->bind(':lname', 'Wagter');
$database->bind(':age', '27');
$database->bind(':gender', 'male');

$database->execute();

echo $database->lastInsertId();

Database.php

<?php
class Database
{

private $host = DB_HOST;
private $user = DB_USER;
private $pass = DB_PASS;
private $dbname = DB_NAME;

private $dbh;
private $error;

private $stmt;

public function __construct()
{

    echo "in class Database";

    $dsn = 'mysql:host' . $this->host . ';dbname=' . $this->dbname;

    $options = array(
        PDO::ATTR_PERSISTENT => true,
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
    );

    try {
        $this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
    } catch (PDOExeption $e) {
        $this->error = $e->getMessage();
    }

}

public function query($query)
{

    $this->stmt = $this->dbh->prepare($query);

}

public function bind($param, $value, $type = null)
{

    if (is_null($type)) {

      switch (true) {
        case is_int($value):
          $type = PDO::PARAM_INT;
          break;
        case is_bool($value):
          $type = PDO::PARAM_BOOL;
          break;
        case is_null($value):
          $type = PDO::PARAM_NULL;
          break;
        default:
          $type = PDO::PARAM_STR;
      }

    }

    $this->stmt->bindValue($param, $value, $type);

}

public function execute()
{
    return $this->stmt->execute();
}

public function resultset()
{
    $this->execute();
    return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}

public function single()
{
    $this->execute();
    return $this->stmt->fetch(PDO::FETCH_ASSOC);
}

public function rowCount()
{
    return $this->stmt->rowCount();
}

public function lastInsertId()
{
    return $this->dbh->lastInsertId();
}


public function beginTransaction()
{
    return $this->dbh->beginTransaction();
}

public function endTransaction()
{
    return $this->dbh->commit();
}

public function cancelTransaction()
{
    return $this->dbh->rollBack();
}

Id take into account the link in the comment given by Your Common Sense,

You can try putting the database name in your query to get things moving.

$database->query('INSERT INTO test.mytable (FName, LName, Age, Gender) 
VALUES (:fname, :lname, :age, :gender)');

Or a USE DATABASE before you do your query.

$database->query("USE DATABASE test");
$database->execute();

Id abandon writing the database wrapper however, with the reasons highlighted in the link above. PDO is pretty good.

Since you get No database selected and database is specified in the DSN string then the error must be there. You have this:

$dsn = 'mysql:host' . $this->host . ';dbname=' . $this->dbname;

You should print it for diagnose. It'll look like this:

mysql:hostlocalhost;dbname=test

Now the typo is clear.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM