我正在尝试从一些开源创建一个新的Web应用程序的MySQLi驱动程序,并且无法通过未定义的属性通知。
Notice: Undefined property: mysqli_result::$rows in index.php
index.php的相关行
$query = $db->query("SELECT * FROM ".DB_PREFIX."setting WHERE store_id='".(int)$config->get('config_store_id')."' ORDER BY store_id ASC");
foreach($query->rows as $setting) {
if(!$setting['serialized']) {
$config->set($setting['key'], $setting['value']);
} else {
$config->set($setting['key'], unserialize($setting['value']));
}
}
我的mysqli控制器
class MySQLii {
private $link;
public function __construct($hostname, $username, $password, $database) {
if(!$this->link = new mysqli($hostname, $username, $password, $database)) {
trigger_error('Could not make a MySQLi database link using '.$username.'@'.$hostname);
exit();
}
mysqli_query($this->link, "SET NAMES 'utf8'");
mysqli_query($this->link, "SET CHARACTER SET utf8");
mysqli_query($this->link, "SET CHARACTER_SET_CONNECTION=utf8");
mysqli_query($this->link, "SET SQL_MODE = ''");
}
public function query($sql) {
if($this->link) {
$resource = mysqli_query($this->link, $sql);
if($resource) {
if($resource instanceof mysqli_result) {
$i=0;
$data = array();
while($result=$resource->fetch_object()) {
$data[$i]=$result;
$i++;
}
$result->close();
$query = new stdClass();
$query->row = isset($data[0]) ? $data[0] : array();
$query->rows = $data;
$query->num_rows = $i;
unset($data);
return $query;
} else { return true; }
} else {
trigger_error('Error: '.mysqli_error($this->link).'<br>Error No: '.mysqli_errno($this->link).'<br>'.$sql);
exit();
}
}
}
和db控制器根据配置设置加载适当的驱动程序,在这种情况下是mysqli驱动程序......
class DB {
private $driver;
public function __construct($driver, $hostname, $username, $password, $database) {
if(file_exists(DIR_DATABASE.$driver.'.php')) {
require_once(DIR_DATABASE.$driver.'.php');
} else {
trigger_error('Could not load database file '.$driver.'.');
exit();
}
$this->driver = new $driver($hostname, $username, $password, $database);
}
public function query($sql) {
return $this->driver->query($sql);
}
public function escape($value) {
return $this->driver->escape($value);
}
public function countAffected() {
return $this->driver->countAffected();
}
public function getLastId() {
return $this->driver->getLastId();
}
}
有没有理由我的index.php没有识别stdClass并且告诉我$ query-&gt;行不是属性?顺便说一句,这个代码(几乎相同)与mysql而不是mysqli工作得很好!