我正在为我的网站制作一个 mysqli 类,但我被卡住了。
这是我的课:
class Database {
private $mysqli = '';
private $params = '';
private $result = '';
function Database($hostname, $username, $password, $database) {
$this->mysqli = new mysqli($hostname, $username, $password, $database);
if ($this->mysqli->connect_errno) {
printf('Error #%s -> %s', $this->mysqli->connect_errno, $this->mysqli->connect_error);
exit;
}
}
public function setQuery($query) {
$this->result = $this->mysqli->prepare($query);
$this->params = array();
return $this;
}
public function setParam($type, $value) {
$this->params[$type] = $value;
return $this;
}
public function execute() {
$params = $this->params;
foreach ($params as $key => $value) {
$this->result->bind_param($key, $value);
}
$user = $this->result->result_metadata()->fetch_assoc();
echo $user['username'];
}
}
我在用着:
Core::$database->setQuery('SELECT * FROM habbo_characters WHERE id = ?')->setParam('i', 1)->execute();
但是我没有输出/空字符串作为输出。我做错了什么?setParam 函数可以正常工作,但事实并非如此。它包含 id 为 1 的用户(sql)。