-2

我正在为我的网站制作一个 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)。

4

1 回答 1

-1

使用 PDO:

<?php
/* Execute a prepared statement by passing an array of values */
$sql = 'SELECT name, colour, calories
    FROM fruit
    WHERE calories < :calories AND colour = :colour';
$sth = $dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$sth->execute(array(':calories' => 150, ':colour' => 'red'));
$red = $sth->fetchAll();
$sth->execute(array(':calories' => 175, ':colour' => 'yellow'));
$yellow = $sth->fetchAll();
?>

更多示例可以在这里找到:http: //php.net/manual/de/pdo.prepare.php

于 2013-03-24T13:37:00.393 回答