I am baffled, I have the following query which should return 4 records but for some reason it is counting 8 records and returning each row twice. I know I could use distinct, but as I also know that there are no duplicate records in this table I don't want to resolve the problem that way but find out what is causing it.

public function get_online_classes_per_dog($entry_id) {

        $sql = mysql_query("SELECT class_number FROM running_orders WHERE entry_id='".$entry_id."' ORDER BY class_number ASC");

        echo mysql_num_rows($sql);

        $results = mysql_fetch_array($sql);

        return $results;

I have echo'd the $sql and run in directly in the database and I get 4 records as I should but echoing the mysql_num_rows gives me 8 and when I run a while or foreach loop on my main page I get each class_number eching out twice (for example it should be 1, 2, 3, 4 but I am getting 1, 1, 2, 2, 3, 3, 4, 4).

Any ideas, never come across anything like this before and is driving me mad??

Recommended Answers

All 5 Replies

What code is calling this function? This function just returns one record.

The code calling the function is

$classes_entered = new entries;
$class_entries = $classes_entered->get_online_classes_per_dog($entries['entry_id']);

This is set within a while loop but not sure what bearing it has on the query in the function as the echo of mysql_num_rows is within the function and all other function calls within the while loop work as expected.

I have been wondering why you thought the query only returned one record and realised I explained things slightly incorrectly - within the running orders table there are duplicate entry_ids (one for each class number entered) but the class numbers aren't duplicated, so in the case of my query above the result will be 4 class numbers for the one entry id. Hope this clarifies it better.

Can we see your while loop

This is a big, complex page of code so I think the best thing (rather than keep trying to post bits of it on here) is that I will strip it right back and see if I can find a cause of the problem like that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.