我正在尝试建立数据库连接并检查表中的现有数据。但是我收到了这个错误,我无法找到原因:
警告:mysqli :: query():无法获取mysqli /usr/share/nginx/www/me/container/class_lib.php 在线 33
警告:mysql_fetch_assoc()预计 参数1为资源,null为给定 /usr/share/nginx/www/me/container/class_lib.php 在线 97
class dbHandler {
static $dbObj = null;
protected $db_host = 'localhost'; #db host
protected $db_username = 'user'; #db username
protected $db_password = 'password'; #db password
protected $db_name = 'db'; #db name
function __construct()
{
# connect if not connected
if(self::$dbObj === null)
{
self::$dbObj = new mysqli($this->db_host, $this->db_username, $this->db_password, $this->db_name)
or die($this->dbObj->error);
}
mysqli_set_charset(self::$dbObj, "utf8");
}
// query: query the db
public function query($query)
{
return self::$dbObj->query($query);
}
}
/*
class userLogin
create user login
*/
class userLogin {
private $username;
private $password;
function __construct($username, $password) {
$this->_dbConn = new dbHandler();
$this->username = $username;
$this->password = $password;
}
public function verifyCredentials() {
if($this->verifyUsername())
{
} else {
exit;
}
if($this->verifyPassword())
{
} else {
exit;
}
}
private function verifyUsername() {
if(!(preg_match('/[^a-z_\-0-9]/i', $this->username)))
{
return true;
}
}
private function verifyPassword() {
$query = "SELECT * FROM tbl_user";
$result = $this->_dbConn->query($query);
$row = mysql_fetch_assoc($result);
var_dump($row);
}
}
我在这里做错了什么?
答案 0 :(得分:4)
你所有的包装器都是以mysqli面向对象的方式,突然间你有了这条线?
$row = mysql_fetch_assoc($result);
您必须使用mysqli结果对象中的fetch_assoc
$result->fetch_assoc()
答案 1 :(得分:0)
您的SQL查询显然失败了。为什么你有一个函数查询来调用我不知道的查询,但我感觉这是你的问题的路由原因。你最好用这样的东西:
class dbHandler extends mysqli {
protected $db_host = 'localhost'; #db host
protected $db_username = 'user'; #db username
protected $db_password = 'password'; #db password
protected $db_name = 'db'; #db name
function __construct()
{
parent::__construct($this->db_host, $this->db_username, $this->db_password, $this->db_name);
if($this->connect_errno)
{
trigger_error('Unable to connect to the database [' . $this->connect_errno . ']: ' . $this->connect_error, E_USER_ERROR);
}
}
public function __destruct()
{
parent::close();
}
}
/*
class userLogin
create user login
*/
class userLogin {
private $username;
private $password;
function __construct($username, $password) {
$this->_dbConn = new dbHandler();
$this->username = $username;
$this->password = $password;
}
public function verifyCredentials() {
if($this->verifyUsername())
{
} else {
exit;
}
if($this->verifyPassword())
{
} else {
exit;
}
}
private function verifyUsername() {
if(!(preg_match('/[^a-z_\-0-9]/i', $this->username)))
{
return true;
}
}
private function verifyPassword() {
$query = "SELECT * FROM tbl_user";
$result = $this->_dbConn->query($query);
if($this->_dbConn->errno)
{
trigger_error('Error fetching users from table. Query: ' . $query . '. Error: ' . $this->_dbConn->error);
return false;
}
if($result->num_rows)
{
while($row = $result->fetch_assoc())
{
var_dump($row);
}
}
}
}
让我知道你是如何继续这样做的。
答案 2 :(得分:0)
所以......我从未给过localhost的mysql用户任何授权。仅来自远程LAN。问题解决了。
答案 3 :(得分:0)
可能你过早关闭连接? DBConnect->close();
?
因此,如果您在此之后尝试执行任何查询,则会收到错误!