<!---- Database File --->
<?php
defined('DB_SERVER') ? null : define("DB_SERVER","localhost");
defined('DB_USER') ? null : define("DB_USER", "faizy");
defined('DB_PASS') ? null : define("DB_PASS", "faizy");
defined('DB_NAME') ? null : define("DB_NAME", "photo_gallery");
class MySQLDatabase {
private $connection;
function __construct() {
$this->open_connection();
}
public function open_connection() {
$this->connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
if(!$this->connection) {
die("Database connection Failed: " .mysqli_error($this->connection));
}else {
$db_select = mysqli_select_db($this->connection, DB_NAME);
if(!$db_select) {
die("Database connection Failed: " .mysqli_error($this->connection));
}
}
}
public function close_connection() {
if(isset($this->connection)) {
mysqli_close($this->connection);
unset($this->connection);
}
}
public function query($sql) {
$result = mysqli_query($sql, $this->connection);
$this->confirm_query($result);
return $result;
}
private function confirm_query($result) {
if(!$result) {
die("Database connection Failed: " .mysqli_error($this->connection));
}
}
}
$database = new MySQLDatabase();
$database->close_connection();
?>
<!----Config FIle which ---->
<?php
defined('DB_SERVER') ? null : define("DB_SERVER","localhost");
defined('DB_USER') ? null : define("DB_USER", "faizy");
defined('DB_PASS') ? null : define("DB_PASS", "faizy");
defined('DB_NAME') ? null : define("DB_NAME", "photo_gallery");
?>
<!--index.html-->
<body>
<?php
require_once '../includes/database.php';
if(isset($database)){ echo "true"; } else { echo "false"; }
echo "<br/>";
echo $database->mysql_prep("It's Working <br />");
$sql = "INSERT INTO users (id, username,password,first_name,last_name) VALUES(1,'walifaizy','12345','wali','faizy')";
$result = $database->query($sql);
?>
</body>
[![Below are the errors][1]][1]
当我在数据库中插入一些东西而不是这些警告和通知即将到来时,任何人都可以建议我如何解决这些错误,我因为这些警告和通知而陷入困境,我正在以正确的方式定义常量,一切都是正确的这些通知即将到来的原因
![]()
答案 0 :(得分:0)
检查第38行...... mysqli_query($ con,&#34;您的查询&#34;)。这是正确的语法。以这种格式编写代码
答案 1 :(得分:0)
在数据库包含文件的末尾,您在打开数据库后立即关闭数据库:
$database = new MySQLDatabase();
$database->close_connection();
因此它没有连接。