当我尝试通过OOP方法构建连接时,我遇到了一些错误。我使用了两个文件databse.php
和config.php
。
这是我的'database.php'
<?php
require_once 'functions.php';
require_once 'config.php';
/**
*
*/
class Database{
private $connection;
public $name;
function __construct(){
$this->open_connection();
}
public function open_connection() {
$this->connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
if (!$this->connection) {
echo mysqli_error($this->connection);
}
}
public function query($sql){
$result_set = mysqli_connect($this->connection, $sql);
while ($result = mysqli_fetch_array($result_set)) {
$this->name = $result['name'];
}
}
}
$db = new Database();
$db->query("SELECT * FROM items");
?>
这里是'config.php'
<?php
defined('DB_SERVER') ? null : define('DB_SERVER', 'localhost');
defined('DB_USERNAME') ? null : define('DB_USERNAME', 'root');
defined('DB_PASSWORD') ? null : define('DB_PASSWORD', 'root');
defined('DB_NAME') ? null : define('DB_NAME', 'cafe');
?>
错误是:
Notice: Use of undefined constant DB_USER - assumed 'DB_USER' in /var/www/html/cafe/includes/database.php on line 17
Notice: Use of undefined constant DB_PASS - assumed 'DB_PASS' in /var/www/html/cafe/includes/database.php on line 17
Warning: mysqli_connect(): (HY000/1045): Access denied for user 'DB_USER'@'localhost' (using password: YES) in /var/www/html/cafe/includes/database.php on line 17
Warning: mysqli_error() expects parameter 1 to be mysqli, boolean given in /var/www/html/cafe/includes/database.php on line 19
Warning: mysqli_connect(): (HY000/1045): Access denied for user 'SELECT * FROM items'@'localhost' (using password: NO) in /var/www/html/cafe/includes/database.php on line 24
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /var/www/html/cafe/includes/database.php on line 25
答案 0 :(得分:0)
按如下方式更改您的代码
[
{
id: 1,
title: 'Robinson Crusoe',
author: 'Daniel Defoe',
references: ['chp1_robincrusoe', 'chp2_robincrusoe'],
},
{
id: 2,
title: 'Gullivers Travels',
author: 'Jonathan Swift',
references: ['chp1_gulliverstravels', 'chp2_gulliverstravels', 'chp3_gulliverstravels'],
},
]
答案 1 :(得分:0)
您连接到某个未定义的值。
$this->connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
DB_USER,DB_PASS未定义
其DB_USERNAME,DB_PASSWORD
defined('DB_USERNAME') ? null : define('DB_USERNAME', 'root');
defined('DB_PASSWORD') ? null : define('DB_PASSWORD', 'root');