phpmysqlmysqliphp-mysqlidb

How do read and loop through these MySQLi results?


I'm currently converting some code from mysql to mysqli. I have downloaded this class: https://github.com/ajillion/PHP-MySQLi-Database-Class

I am trying something as simple as this:

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

That prints out:

Array ( [0] => Array ( [Id] => 100017 ) )

When not using this class, I was using the following code afterwards:

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

However that now generates error:

Message: Trying to get property of non-object

So the results are coming back in an array - how do I read how many results came back and then loop through them? I know this must be simple but I've managed to really confuse myself and time I need a quick solution.

Further information - here is 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;
}

Solution

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

    but better download this one: https://github.com/colshrapnel/safemysql
    it will take you only 2 lines to get your $id

    $sql = "SELECT Id FROM productrates WHERE Type = ?s ORDER BY GrossYearly LIMIT 1";
    $id  = $this->db->getOne($sql, 'handset');