【发布时间】:2023-03-18 09:00:01
【问题描述】:
我想知道我是否使用 mysql 和 php 安全。这是我的课:
class DB{
public $mysqli = null;
public $result = array();
private $_host = 'localhost';
private $_user = 'root';
private $_password = 'root';
private $_db_name = 'DBNAME';
public function __construct()
{
$this->mysqli = new mysqli($this->_host,$this->_user, $this->_password, $this->_db_name);
if ($this->mysqli->connect_errno){
echo "Error MySQLi: (" . $this->mysqli->connect_errno . ") " . $this->mysqli->connect_error;
exit();
}
$this->mysqli->set_charset("utf8");
}
public function __destruct(){
$this->mysqli->close();
}
public function query($query){
$this->result = $this->mysqli->query($query);
if($this->result){
return $this->result;
}else{
return false;
}
}
这种方式与数据库通信是否足够好,或者我应该使用 Doctrine 吗?
我问是因为我的代码中有一些奇怪的东西。如果我 vardump 任何对象包含对 DB 对象的引用,我可以看到:
["_host":"DB":private]=> 字符串(9) "localhost" ["_user":"DB":private]=> 字符串(4) "root" ["_password":"DB":private]=> string(4) "root" ["_db_name":"DB":private]=> string(10) >"DBNAME" }
【问题讨论】:
-
如果你打印对象来查看这个数据是正常的。你的类是一个普通的扩展包装器。安全性伴随着复杂的查询和你的班级“准备”它们的能力——捕捉可能的注入等等(简单地说)。