-1
而不是我已经完成的波纹管,我怎么能直接从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;
}
}
?>
为什么两个不同的apis?以及你在哪里/如何使用该课程? –
是我知道如何填充mysqli_query(???,$ sql)的唯一方法;字段提交SQL – Dylan