在多个 PHP 脚本中引用连接文件

我在理解如何在多个 PHP 脚本中正确引用以下连接文件时遇到了一些麻烦。


数据库配置文件:


配置文件


/**

 * Database config variables

 */

define("DB_HOST", "localhost");

define("DB_USER", "username");

define("DB_PASSWORD", "password");

define("DB_DATABASE", "dbName");

?>

连接文件:


DB_Connect.php


<?php

class DB_Connect {

    private $conn;


    // Connecting to database

    public function connect() {

        require_once 'include/Config.php';


        // Connecting to mysql database

        $this->conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);


        // return database handler

        return $this->conn;

    }

}


?>

PHP 文档的连接头:


以下 PDO 脚本的连接头应该如何编写:


<?php




{


  $options = [

    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,

    PDO::ATTR_EMULATE_PREPARES => false,

  ];


  $conn = new PDO("mysql:host=$hostdb;dbname=$namedb;charset=$charset", $userdb, $passdb, $options);


  $stmt = $conn->prepare("SELECT `column1`

    FROM `Table1` ");


  $stmt->execute([]);


  $row = $stmt->fetchAll(PDO::FETCH_ASSOC);


  echo json_encode($row);

}


?>


四季花海
浏览 672回答 2
2回答

万千封印

首先,这个类 DB_Connect完全没有意义。理论上它添加了一些抽象的“抽象”,但实际上它只是一段无用的代码。所以只是摆脱它。然后创建一个名为的文件pdo.php并添加以下代码(基于我的How to connect to MySQL using PDO canonical example):$host = '127.0.0.1';$db&nbsp; &nbsp;= 'dbname';$user = 'username';$pass = 'password';$charset = 'utf8mb4';$options = [&nbsp; &nbsp; \PDO::ATTR_ERRMODE&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; => \PDO::ERRMODE_EXCEPTION,&nbsp; &nbsp; \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,&nbsp; &nbsp; \PDO::ATTR_EMULATE_PREPARES&nbsp; &nbsp;=> false,];$dsn = "mysql:host=$host;dbname=$db;charset=$charset";try {&nbsp; &nbsp; &nbsp;$pdo = new \PDO($dsn, $user, $pass, $options);} catch (\PDOException $e) {&nbsp; &nbsp; &nbsp;throw new \PDOException($e->getMessage(), (int)$e->getCode());}然后只需将此文件包含在需要数据库交互的任何脚本中:<?phprequire $_SERVER['DOCUMENT_ROOT'].'/includes/pdo.php';$stmt = $pdo->query("SELECT `column1` FROM `Table1` ");$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);echo json_encode($rows);注意那个$_SERVER['DOCUMENT_ROOT']东西。它用于使您的文件可从任何目录使用。我在另一篇关于使用正确文件系统路径的文章中对此进行了解释

青春有我

class DB_PDO_Connect {&nbsp; &nbsp; private $db;&nbsp; &nbsp; // Connecting to database&nbsp; &nbsp; public function connect() {&nbsp; &nbsp; &nbsp; &nbsp; if(!isset($this->db)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; require_once 'include/Config.php';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->db = new PDO("mysql:dbname=DB_DATABASE;host=DB_HOST;", DB_USER, DB_PASSWORD,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->db->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING ); // debug&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return $this->db;&nbsp; &nbsp; }}用$myPDO =new DB_PDO_Connect();$conn=$myPDO->connect();$stmt = $conn->prepare("SELECT `column1` FROM `Table1` ");//...
打开App,查看更多内容
随时随地看视频慕课网APP