Jump to content

Looping with object-oriented MYSQL function


V

Recommended Posts

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.. :confused:

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.