lalnfl Posted July 21, 2010 Share Posted July 21, 2010 This is just a piece of coding, hopefully you can understand what I am trying to get and help me get it: $sql = mysql_query("SELECT * FROM Topic WHERE category_id='$category_id' ORDER BY id DESC LIMIT 10"); while ($row = mysql_fetch_array($sql)){ $result = mysql_num_rows($sql); $topic_id = $row['id']; $topic_name = $row['topic_name']; $posts = $row['posts']; $mem_id = $row['mem_id']; $get_mem_name = mysql_query("SELECT * FROM Member WHERE id='$mem_id'"); $get_mem_row = mysql_fetch_array($get_mem_name); $mem_name = $get_mem_row['username']; $get_reply_mem = mysql_query("SELECT * FROM Reply WHERE topic_id='$topic_id'"); $get_results = mysql_num_rows($get_reply_mem); if ($get_results >= 1){ $test = "works"; } else { $test = "doesn't work"; } Explain to me why the above coding is getting the else statement "doesn't work" instead of the if statement "works" and tell me what I should do different to get what I am looking for Link to comment https://forums.phpfreaks.com/topic/208360-help-with-this-code/ Share on other sites More sharing options...
dreamwest Posted July 21, 2010 Share Posted July 21, 2010 $sql = mysql_query("SELECT * FROM `Topic` WHERE `category_id`='{$category_id}' ORDER BY `id` DESC LIMIT 10")or die(mysql_error()); //remove or die() after testing $num = mysql_num_rows($sql); if($num ==0){ echo 'No results'; die(); } while ($row = mysql_fetch_assoc($sql)){ $topic_id = $row['id']; $topic_name = $row['topic_name']; $posts = $row['posts']; $mem_id = $row['mem_id']; $get_mem_name = mysql_query("SELECT * FROM `Member` WHERE `id`='{$mem_id}' "); $get_mem_row = mysql_fetch_assoc($get_mem_name); $mem_name = $get_mem_row['username']; $get_reply_mem = mysql_query("SELECT * FROM `Reply` WHERE `topic_id`='{$topic_id}' "); $get_results = mysql_num_rows($get_reply_mem); if ($get_results >= 1){ $test = "works"; }else{ $test = "doesn't work"; } echo $test."<br>"; }//end while Link to comment https://forums.phpfreaks.com/topic/208360-help-with-this-code/#findComment-1088885 Share on other sites More sharing options...
lalnfl Posted July 21, 2010 Author Share Posted July 21, 2010 So can you tell me what I did wrong? Link to comment https://forums.phpfreaks.com/topic/208360-help-with-this-code/#findComment-1088898 Share on other sites More sharing options...
dreamwest Posted July 21, 2010 Share Posted July 21, 2010 - I wouldnt use fetch_array if your defining the field anyway, use fetch_assoc - $result = mysql_num_rows($sql); is in a while loop and shouldnt be - no closing while loop bracket etc.. Link to comment https://forums.phpfreaks.com/topic/208360-help-with-this-code/#findComment-1088911 Share on other sites More sharing options...
lalnfl Posted July 21, 2010 Author Share Posted July 21, 2010 So when you tested it with your code, the result that came out was test "works" right? Link to comment https://forums.phpfreaks.com/topic/208360-help-with-this-code/#findComment-1089228 Share on other sites More sharing options...
dreamwest Posted July 21, 2010 Share Posted July 21, 2010 I never tested it, but its correct. Ill only output "Works" if theres a row in this query: mysql_query("SELECT * FROM `Reply` WHERE `topic_id`='{$topic_id}' "); Link to comment https://forums.phpfreaks.com/topic/208360-help-with-this-code/#findComment-1089292 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.