我试图创建一个类似于大多数 cms 的数据库类,所以我在一个函数中建立连接并返回连接对象以用于类中其他函数的查询..
<?php
class my_database {
var $host = 'localhost';
var $user = 'prince';
var $pass = 'abcd1234wxyz';
var $db = 'world';
function __construct() {
$this->db_connect();
}
function db_connect() {
$mysqli = mysqli_connect($host,$user,$pass,$db);
/* check connection */
if (!$mysqli) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
return $mysqli;
}
function query($query) {
$mysqli = $this->db_connect();
if ($result = mysqli_query($mysqli, $query)) {
while ($row = mysqli_fetch_row($result)) {
echo "<pre>";
print_r($row);
echo "</pre>";
}
mysqli_free_result($result);
} else
echo "fails"; exit;
}
}
$database = new my_database;
/*************** Class Ends ****************************/
function get_results() {
global $database;
$database->query("select * from city limit 10");
}
get_results();
?>
它总是输出fails
,我是 Oops & mysqli 的新手,也许我遗漏了一些明显的东西。请帮忙 !