我在理解如何在多个 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);
}
?>
万千封印
青春有我