PDO:数据库凭据

时间:2016-05-27 09:17:55

标签: php mysql oop pdo

我正在尝试创建自己的数据库类。虽然我已经声明了所有凭据,但错误告诉我没有指定数据库。

  

致命错误:未捕获PDOException:SQLSTATE [3D000]:无效的目录   名称:1046未选择数据库   /var/www/html/jwagter/work/database/Database.php:69堆栈跟踪:#0   /var/www/html/jwagter/work/database/Database.php(69):   PDOStatement-> execute()#1   /var/www/html/jwagter/work/database/tutorial.php(22):   数据库 - >执行()#2 {main}抛出   第69行/var/www/html/jwagter/work/database/Database.php

的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();
}

2 个答案:

答案 0 :(得分:0)

我会考虑您的常识给出的评论中的链接

您可以尝试将数据库名称放在查询中以使事情发生变化。

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

或在您进行查询之前使用数据库。

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

我放弃编写数据库包装器,但上面的链接中突出显示了原因。 PDO非常好。

答案 1 :(得分:0)

由于未选择数据库并且在DSN字符串中指定了数据库,因此必须存在错误。你有这个:

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

您应该将其打印出来进行诊断。它看起来像这样:

mysql:hostlocalhost;dbname=test

现在输入错误很清楚。