[英]PHP:Mysqli_real_escape_string
i'm having an issue with this piece of code and i cant see where the problem . 我这段代码有问题,我看不出问题出在哪里。
<?php
require_once("config.php");
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 Problem : ".mysqli_connect_errno()." : ".mysqli_connect_error());
}
}
public function close_connection(){
if(isset($this->connection)){
mysqli_close($this->connection);
unset($this->connection);
}
}
public function query($sql){
$sql=mysql_prep($sql);
$result=mysqli_query($this->connection,$sql);
confirm_query($result,$sql);
return $result;
}
public function mysqli_prep($string){
global $connection;
$safe_string=mysqli_real_escape_string($this->connection,$string);
return $safe_string;
}
private function confirm_query($result,$sql){
if(!$result){
Die("Database query Problem : ".$sql);
}
}
}
$database=new MySQLDatabase();
$db=& $database;
?>
And i try to run the code bellow on Index.php 我尝试在Index.php上运行以下代码
<?php
require_once("../includes/database.php");
echo "Is this working?";
echo $database->mysqli_prep("sss");
?>
But I receive this Error 但是我收到这个错误
Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, null given in \\includes\\database.php on line 29 警告:mysqli_real_escape_string()期望参数1为mysqli,第29行的\\ includes \\ database.php中给出的null
The error is saying that the $this->connection
you use in MySQLDatabase::mysqli_prep()
is null
. 错误是说您在MySQLDatabase::mysqli_prep()
使用的$this->connection
为null
。 That's because it was never initialized. 那是因为它从未被初始化。
Your class constructor needs to start with two underscores instead of one: 您的类构造函数需要以两个下划线而不是一个下划线开头:
function __construct(){
$this->open_connection();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.