Maison >développement back-end >tutoriel php >php mysql操作问题
数据库链接成功返回,在执行mysqli_query时抛出异常,求解!
错误信息:
Fatal error: Uncaught exception 'DB_Exception' with message '更新失败:No database selected' in...
php脚本:
include_once("pay/CommonUtil.php");
include_once dirname(__FILE__).'/db/DbFactory.php';
$db = DbFactory::getInstance();
$sql = "select * from z_user";
$data = $db->query($sql);
var_dump($data);
?>
数据库单例成功返回
class DbFactory {
private static $db = array();
public static function getInstance($dbKey = 'DEFAULT') {
if (array_key_exists($dbKey, self::$db)) {
return self::$db[$dbKey];
} else {
$newdb = new MysqliDb($dbKey);
if ($newdb->connect()) {
self::$db[$dbKey] = $newdb;
return $newdb;
} else {
return false;
}
}
}
}
$qrs = mysqli_query($this->_conn, $sql, $quick ? MYSQLI_USE_RESULT : MYSQLI_STORE_RESULT);
if (!$qrs) {
throw new DB_Exception('查询失败:' . mysqli_error($this->_conn));
} else {
return $qrs;
}
错误信息很明确:No database selected
没有选择数据库
跟了一下代码,原来是database key写错了,谢谢先辈