Adrian Fischer
Adrian Fischer

Reputation: 119

OOP select query works but unsure how to process the results

I'm turning my hand to OOP and am having trouble getting my head around how to process the results. This is a query I'm using.

   $rows = $db->select("SELECT * FROM tblriskregister 
    WHERE riskSessionId=$riskSessionId ");

I can print out elements of the results by using $rows[0]['aname']. But I don't know how to iterate through the results. Everything is in $rows but no matter what I have tried I can't seem to iterate through. I've tried

    while($rows->num_rows >0) and
    $rows->fetch_assoc()

but everything I try doesn't work. The initial query to the database has returned everything to $rows as an array but I can't work out how to go through it and print out my rows.

Upvotes: 0

Views: 44

Answers (2)

undefined
undefined

Reputation: 690

If you're trying to use OOP, use objects instead of arrays to handle the results.

$result = mysqli_query("select * from table");

while($row = mysqli_fetch_object($result) {
   echo $row->fileName1;
   echo $row->fieldName2;
}

The function mysqli_fetch_object automatically loops through the results of the query.

Upvotes: 0

Felippe Duarte
Felippe Duarte

Reputation: 15131

If you have an array with all results, just iterate it this way:

foreach($rows as $row) {
    echo $row['field_name'];
    echo $row['other_field_name'];
}

Upvotes: 1

Related Questions