guymclaren Posted December 12, 2009 Share Posted December 12, 2009 I am trying to create an alternative if the answer is NULL, How or what am I doing wrong? 1. If ($status == 0){ 2. $sqlq="SELECT * FROM cocats INNER JOIN companies ON companies.id = cocats.co WHERE cocats.cat='".$cat." AND companies.status = '3' ORDER BY RAND() LIMIT 0,3"; 3. $objRS = mysql_query($sqlq); 4. while($row = mysql_fetch_array($objRS, MYSQL_ASSOC)) { 5. $status = $row["status"]; 6. $profile = $row['profile']; 7. } 8. If ($row == Null) { 9. $sqlq="SELECT * FROM companies WHERE status = '3' ORDER BY RAND()LIMIT 0,3";} 10. $objRS = mysql_query($sqlq); 11. while($row = mysql_fetch_array($objRS, MYSQL_ASSOC)) { 12. $status = $row["status"]; 13. $profile = $row['profile'] 14. } 15. } Link to comment https://forums.phpfreaks.com/topic/184890-what-am-i-doing-wrong-if-while-else/ Share on other sites More sharing options...
cags Posted December 12, 2009 Share Posted December 12, 2009 $row cannot be NULL, your while loop will only be entered if $row doesn't evaluate to FALSE (which NULL does). The only value that can be NULL is a field in the $row array, such as $row['status']. Link to comment https://forums.phpfreaks.com/topic/184890-what-am-i-doing-wrong-if-while-else/#findComment-976018 Share on other sites More sharing options...
guymclaren Posted December 12, 2009 Author Share Posted December 12, 2009 $row cannot be NULL, your while loop will only be entered if $row doesn't evaluate to FALSE (which NULL does). The only value that can be NULL is a field in the $row array, such as $row['status']. OK so if I have no results, how do I change my query, you see if I say if status == Null then it gives me the true loop and adds the false loop at the end. Link to comment https://forums.phpfreaks.com/topic/184890-what-am-i-doing-wrong-if-while-else/#findComment-976019 Share on other sites More sharing options...
wildteen88 Posted December 12, 2009 Share Posted December 12, 2009 You should be using mysql_num_rows to check if your query returned any results. Link to comment https://forums.phpfreaks.com/topic/184890-what-am-i-doing-wrong-if-while-else/#findComment-976020 Share on other sites More sharing options...
guymclaren Posted December 12, 2009 Author Share Posted December 12, 2009 Thank you Link to comment https://forums.phpfreaks.com/topic/184890-what-am-i-doing-wrong-if-while-else/#findComment-976034 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.