我不是直接从dbconfig.php获取$conn
,而是至少在$ link的位置使用它,以便我可以避免重复代码。
$link = mysqli_connect("localhost", "root", "", "test");
mysqli_query($link,$sql);
我正在使用以下dbconfig.php
<?php
class Database
{
private $host = "localhost";
private $db_name = "test";
private $username = "root";
private $password = "";
public $conn;
public function dbConnection()
{
$this->conn = null;
try
{
$this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $exception)
{
echo "Connection error: " . $exception->getMessage();
}
return $this->conn;
}
}
?>
答案 0 :(得分:0)
使用全球竞争对手,确保您可以全局访问
示例代码(来自wordpress)
define('DB_NAME', 'wp');
define('DB_USER', 'root');