0

我目前正在将一些代码从 mysql 转换为 mysqli。我已经下载了这个类: https ://github.com/ajillion/PHP-MySQLi-Database-Class

我正在尝试这样简单的事情:

    $params = array('handset');
    $rs = $this->db->rawQuery("SELECT Id FROM productrates WHERE Type = ? ORDER BY GrossYearly ASC LIMIT 0,1 ", $params);   
    print_r($rs);

打印出来:

数组([0] => 数组([Id] => 100017))

当不使用此类时,我后来使用了以下代码:

        if ($rs->num_rows != 0) {
        $row = $rs->fetch_assoc();

但是,现在会产生错误:

消息:试图获取非对象的属性

所以结果以数组的形式返回 - 我如何读取返回了多少结果然后循环遍历它们?我知道这一定很简单,但我真的让自己感到困惑,我需要一个快速的解决方案。

更多信息 - 这里是 rawQuery:

public function rawQuery($query, $bindParams = NULL) 
{
    $this->_query = filter_var($query, FILTER_SANITIZE_STRING);
    $stmt = $this->_prepareQuery();

    if (gettype($bindParams) === 'array') {
        $params = array('');        // Create the empty 0 index
        foreach ($bindParams as $prop => $val) {
            $params[0] .= $this->_determineType($val);
            array_push($params, $bindParams[$prop]);
        }

                call_user_func_array(array($stmt, "bind_param"),$this->refValues($params));

    }

    $stmt->execute();
    $this->reset();

    $results = $this->_dynamicBindResults($stmt);
    return $results;
}
4

3 回答 3

1
if ($rs) {
    $row = $rs[0];
}

但最好下载这个:https
://github.com/colshrapnel/safemysql 它只需要 2 行就可以得到你的 $id

$sql = "SELECT Id FROM productrates WHERE Type = ?s ORDER BY GrossYearly LIMIT 1";
$id  = $this->db->getOne($sql, 'handset');   
于 2013-01-14T20:43:19.323 回答
1

您的函数将一个数组返回到一个数组中:) 所以要显示第一个:

$FirstArray = $rs[0];

要显示此数组中的所有项目:

foreach ($FirstArray as $result)
    echo $result;

希望能帮助到你 :)

于 2013-01-14T20:47:37.917 回答
-2

PHP-MySQLi-Database-Class 中没有积极的开发。我不认为你应该浪费你的时间。无论如何,它留给你。

于 2013-01-14T20:46:27.617 回答