0

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;
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
James Wilson
  • 809
  • 3
  • 14
  • 25

3 Answers3

1
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');   
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
1

Your function returns an array into an array :) So to display the first one :

$FirstArray = $rs[0];

To display all the items in this array :

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

Hope it helps :)

Xavier
  • 3,919
  • 3
  • 27
  • 55
-2

There is no active development in PHP-MySQLi-Database-Class. I dont think you should waste your time.. Anyway, it is left to you.

  • 1
    I don't think I need active development. I just need the simple stuff working and if I need to add anything to the class over time I can. – James Wilson Jan 14 '13 at 20:56
  • Is there a MySQLi class or similar you recommend then? I feel like now is the time to update ALL my mysql code and secure my pages more – James Wilson Aug 27 '13 at 08:42