简体   繁体   English

建立数据库连接PDO时出错

[英]Error establishing a database connection PDO

I am using PDO to connect to MySQL 我正在使用PDO连接到MySQL

include_once('connection_insert.php');
global $host, $dbname, $user, $pass;
$DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
$DBH->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); ///*** for error handling

function insert_terms(){
  global $DBH;
  $STH = $DBH->prepare("INSERT IGNORE INTO table
  (name,state) 
  value (?,?)");
  return $STH;
  $DBH = Null;// Closing Connection
}

But i am facing error: 但我面临错误:

Error establishing a database connection 建立数据库连接时出错

My question is, is this the right way use global $DBH; 我的问题是,这是使用global $DBH;的正确方法global $DBH; and closing connection $DBH = Null;// Closing Connection 和关闭连接$DBH = Null;// Closing Connection

EDIT 编辑

connection_insert.php connection_insert.php

$host = 'localhost';
$user = 'username';
$pass = 'password';
$dbname = 'db';

No, using global variables is bad practice ( dont look on Wordpress & and others uses global variables). 不,使用global变量是不好的做法(不要看Wordpress和其他人使用全局变量)。 The more complex the structure (architecture) application - the more terrible the consequences might be of using globals. 结构(体系结构)应用程序越复杂 - 使用全局变量的后果就越可怕。 You can find a lot of information why they are evil.Instead its you can use pattern Registry (for example) for accessing "global" variables in application. 你可以找到很多信息,为什么它们是邪恶的。相反它你可以使用模式注册表 (例如)来访问应用程序中的“全局”变量。 In short, lots of choices. 简而言之,有很多选择。

Сoncerning your code. Сoncerning您的代码。 Params of connecting ( $host , $dbname , $user , $pass ) are defined? 连接的参数( $host$dbname$user$pass )是否已定义?

See also Global or Singleton for database connection? 另请参阅Global或Singleton进行数据库连接? and php.net . php.net

EDIT Example : 编辑示例:

Index.php 的index.php

require_once 'Database.php';

$DB = new Dababase();
$DB->prepare("INSERT IGNORE INTO table (name,state) value (:name,:state)");
$database->bind(':name', 'NAME');
$database->bind(':state', 'STATE');
$DB->execute();

Config.php config.php文件

define("DB_HOST", "localhost");
define("DB_USER", "username");
define("DB_PASS", "password");
define("DB_NAME", "database");

Database.php 为database.php

require_once 'config.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(){
    // Set DSN
    $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
    // Set options
    $options = array(
        PDO::ATTR_PERSISTENT    => true,
        PDO::ATTR_ERRMODE       => PDO::ERRMODE_EXCEPTION
    );
    // Create a new PDO instanace
    try{
        $this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
    }
    // Catch any errors
    catch(PDOException $e){
        $this->error = $e->getMessage();
    }
  } 

  public function prepare($query){
    $this->stmt = $this->dbh->prepare($query);
  }  

  public function query($query){
     return $this->dbh->query($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 execute(){
     return $this->stmt->execute();
   } 

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

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

 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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