Page is a not externally linkable
- Code, Content, and Presentation
-- Databases
---- problem with mysql fetch array


rocknbil - 3:27 pm on Jul 28, 2010 (gmt 0)


The result I get is 5520 and not 5205.


Add an additional echo, with the ID and title and you'll find the count value should align with the right records. I think you'll only need to add an order by clause to the select (order by the date field?)

I recreated the tables you described, and added the records *in the order* you specified so my output is by record ID, which may not be the case in your actual database.

$query = "select (select count(*) from novice_komentarji where novice_komentarji.novicaID=novice.id) as postcount, novice.id, novice.naslov from novice order by novice.datum desc";


$asql = mysql_query($query) or die (mysql_error());
while ($abc = mysql_fetch_array($asql)) {
if($abc['postcount'] > 0) {
echo "id: " . $abc['id'] . " title: " . $abc['naslov'] . " " . $abc['postcount'] . " comments<br>";
}
else {
echo "No new comments<br>";
}
}


Thread source:: http://www.webmasterworld.com/databases_sql_mysql/4175730.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com