【发布时间】:2013-09-26 14:20:54
【问题描述】:
这些代码有什么问题?我对mysqli非常陌生。我总是收到这个错误
警告:mysqli_query() 期望参数 1 为 mysqli,在第 11 行的 C:\localhost\path\core.php 中给出 null 错误,查询失败!!!
我测试了连接并且它正在工作并且连接,所以我不明白为什么我总是看到这个警告并且我的查询失败了。我还检查了查询,它是正确的。这是我的代码:
configuration.php
<?php
$dbhost = 'xxx';
$dbuser = 'xxx';
$dbpass = 'xxx';
$dbname = 'xxx';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname) or die ('Error connecting to mysql');
?>
core.php
<?php
require_once('configuration.php');
class Core {
public $coreTable;
public $result;
public $num_rows;
function Execute($query) {
$this->result = mysqli_query($conn,$query) or die('Error, query failed!!!'); //line 11
$this->row_cnt = mysqli_num_rows( $this->result );
}
}
?>
【问题讨论】:
-
$conn 变量超出范围,因为它不是核心类的一部分。
-
使用 MySQLi 作为对象,甚至比程序风格更好。 =>
class CoreDatabase extends \MySQLi { ... };