Mr Chris Posted May 26, 2009 Share Posted May 26, 2009 Hi All, Can someone please help me, I think i've gone mad. On my site I run a simple query: $result = mysql_query("SELECT* from tble where id = 1"); if(mysql_num_rows($result) > 0) { $row = mysql_fetch_array($result); echo '<h1 class="fr">'.$row['name'].'</h1> <div style="clear:right"></div>'; } else { echo ''; } Now if the criteria is matches in the database I get the details returned, but if there are no reulys found I get the message: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource But surely my } else { should stop this happening? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/159674-solved-query-help-no-results-founds/ Share on other sites More sharing options...
JonnoTheDev Posted May 26, 2009 Share Posted May 26, 2009 You have an error in your query hence no database resource being returned. I can see it straight away: Bad $result = mysql_query("SELECT* from tble where id = 1"); Good $result = mysql_query("SELECT * FROM tble WHERE id = '1'"); You should also catch query errors i.e. if(!$result = mysql_query("SELECT * FROM tble WHERE id = '1'")) { die(mysql_error()); } Quote Link to comment https://forums.phpfreaks.com/topic/159674-solved-query-help-no-results-founds/#findComment-842146 Share on other sites More sharing options...
Adam Posted May 26, 2009 Share Posted May 26, 2009 Actually the space between the star and SELECT shouldn't cause a problem - or at least it doesn't on my version of MySQL (5.0.45) and being as 1 is an integer that shouldn't case a problem either - although it could be possible with different data types. Try the mysql_error() method shown and see what problems there are for you. Also is that all the code or is it possible there's a query later causing the error? Quote Link to comment https://forums.phpfreaks.com/topic/159674-solved-query-help-no-results-founds/#findComment-842149 Share on other sites More sharing options...
Mr Chris Posted May 26, 2009 Author Share Posted May 26, 2009 Hi All, I've tidied up my query, but am still having problems: $query_one = "SELECT strapline from cfm_seo WHERE site_id = ".SITEID." AND section_id = ".$subSecID.""; $result = mysql_query($query_one) OR die(mysql_error()); while ($row = mysql_fetch_array($result)) { $strapline = $row['strapline']; echo '<h1 class="fr">'.$strapline.'</h1> <div style="clear:right"></div>'; } I get this message You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 When I simply want nothing to show if it does not return any results? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/159674-solved-query-help-no-results-founds/#findComment-842153 Share on other sites More sharing options...
Dathremar Posted May 26, 2009 Share Posted May 26, 2009 $query_one = "SELECT strapline from cfm_seo WHERE site_id = '".SITEID."' AND section_id = '".$subSecID."'"; Try this. I put single quotes around the variables Quote Link to comment https://forums.phpfreaks.com/topic/159674-solved-query-help-no-results-founds/#findComment-842155 Share on other sites More sharing options...
Mr Chris Posted May 26, 2009 Author Share Posted May 26, 2009 That's worked, thanks. Wierd one though as on certain pages a result was returned! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/159674-solved-query-help-no-results-founds/#findComment-842157 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.