Number found: Fatal error: Call to a member function fetch_assoc() on a non-object in /home/mysite/public_html/cat_results.php on line 43 Line 43 is $row = $result->fetch_assoc() <?php include 'conn_mysqli.inc.php'; $query = "SELECT title, price FROM mammals08 ORDER BY created DESC "; $result = $db->query( $query ); $num_results = $result->num_rows; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>cat_results</title> </head> <body> <table> <?php echo "<p>Number found: ".$num_results."</p>"; for ($i = 0; $i <= 6; $i++) { $row = $result->fetch_assoc() ?> <tr> <td><?php echo $row['title']; ?></td> <td><?php echo $row['price']; ?></td> </tr> <?php } ?> </table> </body></html> PHP:
Thanks for your reply. I did what you said and tested it. All I get is the same error as mentioned above. Fatal error: Call to a member function fetch_assoc() on a non-object in /home/mobielf1/public_html/cat_results.php on line 43
Why are you using for a loop??, use a while loop and within your query add to the end LIMIT 6 - so it displays only 6 <?php error_reporting(E_ALL); $limit = 6; $query = "SELECT title, price FROM mammals08 ORDER BY created DESC LIMIT {$limit}"; $result = mysql_query($query) or trigger_error('Unable to query: '.mysql_error(), E_USER_ERROR); $num_results = mysql_num_rows($query); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>cat_results</title> </head> <body> <table> <?php echo "<p>Number found: {$num_results}</p>"; while ($row = mysql_fetch_assoc($result)) : ?> <tr> <td><?php echo $row['title']; ?></td> <td><?php echo $row['price']; ?></td> </tr> <?php endwhile; ?> </table> </body></html> PHP:
I just noticed this is not providing a number found. <?php echo "<p>Number found: ".$num_results."</p>";
I tried that to earlier and received the same error. <table> <?php echo "<p>Number found: ".$num_results."</p>"; //$i = 1; //while ($i <= 5) { //$row = $result->fetch_assoc(); //$i++; echo $mysqli->error; for ($i = 0; $i <= 6; $i++) { $row = $result->fetch_assoc(); ?> <tr> <td><?php echo $row['title']; ?></td> <td><?php echo $row['price']; ?></td> </tr> <?php } ?> </table> </body></html> PHP:
Hmm.. Well non-object means that MySQL isn't returning data so it's logical to start there. Make sure that your query works via PHPMYADMIN: SELECT title, price FROM mammals08 ORDER BY created DESC Code (markup): Also make sure that you have the proper MySQL table selected in your database connection. As for your code, I would advise that you do the LIMIT 6; mentioned above as it will improve speed and use less resources. The way you're currently utilizing retrieves all the records, loads them into a PHP resource, then takes only 6 of whatever you retrieved. Using LIMIT will only retrieve the 6 results that you want, which in turn saves resources and has the speed boost as mentioned. If the above trials don't work, it may be an issue with your mysql library. If it doesn't work and if you would like assistance from me, feel free to add me on MSN:
Thanks people for your help. danx10 I'm sorry, I totally glossed over your suggestion to LIMIT 6. I am so new at this I didn't know I could do that. That looks simpler and I will do it that way. po_taka I tried echo $db->error; It gave me a meaningful error message: Unknown column 'created' in 'order clause'. When I added ORDER BY created DESC, I did not know I had to do something to the table in my database. zeOxify I think your analysis was correct and I will let you know about further assistants , thanks.