V Posted June 18, 2010 Share Posted June 18, 2010 Good morning everyone! - in the same time zone Lately I've been processing my dreams through php code, it's so weird. Anyways, I used to connect with the original MYSQL extension and then I read that the object-oriented method is better. I used that but I'm not getting the same results. For example I have a categories table and a topics table. I'm using while loops to display all categories and each topic that belongs to each one. The object-oriented code I'm using is.. require_once("/functions/db_fns.php"); $connection = dbConnect(); //db connection function //prepare Categories query $sql = "SELECT * FROM categories"; $result = $connection->query($sql) or die(mysqli_error($connection)); while ($row = $result->fetch_assoc()) { echo strtoupper("<li>{$row["cat_name"]}</li>"); //prepare Topics query $sql = "SELECT * FROM topics WHERE cat_id = {$row["cat_id"]}"; $result = $connection->query($sql) or die(mysqli_error($connection)); while ($row = $result->fetch_assoc()) { //find out how many records were retrieved $numRows = $result->num_rows; $topic_id=$row['topic_id']; $topic_name=$row['topic_name']; $topic_date=$row['topic_date']; $numRows = $result->num_rows; echo "<a href=\"single_topic.php?topic=" . urlencode($topic_id) . "\"> $topic_name</a> "; echo $numRows; }//end topic while }//end category while With the original MYSQL extension I used to get all the categories and related topics. But now I get just 1 catgeory and 1 topic. I'm trying to figure out why they're not all displaying.. Link to comment https://forums.phpfreaks.com/topic/205175-looping-with-object-oriented-mysql-function/ Share on other sites More sharing options...
KevinM1 Posted June 18, 2010 Share Posted June 18, 2010 The lack of formatting (read: indentation) makes it hard to read, but it looks like you're overwriting $result in your inner loop. Give it a more meaningful name to make it a separate variable from the $result in your outer loop. Link to comment https://forums.phpfreaks.com/topic/205175-looping-with-object-oriented-mysql-function/#findComment-1073971 Share on other sites More sharing options...
V Posted June 18, 2010 Author Share Posted June 18, 2010 Thank you Nightslyr! It's works perfectly now. Yes, sorry about the lack of indention, I need to get used to it. Link to comment https://forums.phpfreaks.com/topic/205175-looping-with-object-oriented-mysql-function/#findComment-1073977 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.