I have a mysql query like this:
$topicreplycount = mysql_query('SELECT COUNT(DISTINCT post_msg_id) FROM phpbb_attachments WHERE topic_id = '.$row['topic_id'].' AND post_msg_id < '.$row['post_msg_id']);
and hope to get the result using something like this:
$topicreplycount = mysql_result($topicreplycount);
so lets say there are 4 post_msg_ids like this in the table:
2956 2957 2957 2958
and the current $row['post_msg_id'] is 2958, there are 3 post_msg_ids that are less than the one I am comparing it to, and only 2 Distinct post_msg_ids since two of them are the same, I am expecting "2" to be returned
but it doesn't seem to work, am I doing this correctly? I am not getting anything returned in the $topicreplycount variable at all using this method
I am pretty sure usign mysql_result($topicreplycount) must be wrong but don't know what to use instead
Try this:
$sql = mysql_query('SELECT COUNT(DISTINCT post_msg_id) AS num_topics FROM phpbb_attachments WHERE topic_id = '.$row['topic_id'].' AND post_msg_id < '.$row['post_msg_id']);
$result = mysql_result($sql);
$row = mysql_fetch_array($result) or die(mysql_error());
echo $row['num_topics'];
Using mysql_result you need to specify at least the row number so
$topicreplycount = mysql_result($topicreplycount,0);
should work. As there will only ever be one result returned because you used the COUNT() function this will be fine.
However, it is recommended NOT to use the mysql_ family of functions. They are effectively deprecated (not officially but even the PHP docs recommends not to use them). You should switch to mysqli_ or, perhaps better still, PDO.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.