shane85 Posted August 17, 2009 Share Posted August 17, 2009 hey guys...I have this line in my code $result = mysql_query("SELECT location_id, ticker FROM host_locations WHERE location_id='2'")or die(mysql_error()); if(@mysql_num_rows($result)==0){ die('No data available'); } and what I am trying to achieve is that if the row TICKER in host_locations is empty, to die and not load the rets of the page...however even with it being empty, the above code seems to do nothing except load the page (rss page) with just blank data but still setup like an rss page...whats wrong? Quote Link to comment https://forums.phpfreaks.com/topic/170684-if-mysql_num_rows-0-not-working/ Share on other sites More sharing options...
rondog Posted August 17, 2009 Share Posted August 17, 2009 isnt it just if (mysql_num_rows($result) == 0) Not sure what the @ is doing Quote Link to comment https://forums.phpfreaks.com/topic/170684-if-mysql_num_rows-0-not-working/#findComment-900178 Share on other sites More sharing options...
shane85 Posted August 17, 2009 Author Share Posted August 17, 2009 I took the @ out and it still displays the page and doesnt die...is it because im not specifically mentioning the ticker row in that statement??? sorry been trying to figure this out the whole weekend...still not working ahhh Quote Link to comment https://forums.phpfreaks.com/topic/170684-if-mysql_num_rows-0-not-working/#findComment-900185 Share on other sites More sharing options...
rondog Posted August 17, 2009 Share Posted August 17, 2009 instead of die try exit - http://us.php.net/exit Quote Link to comment https://forums.phpfreaks.com/topic/170684-if-mysql_num_rows-0-not-working/#findComment-900186 Share on other sites More sharing options...
shane85 Posted August 17, 2009 Author Share Posted August 17, 2009 tried...oof..same thing happens...I dont see why it isnt exiting the page?? doesnt make sence?? Quote Link to comment https://forums.phpfreaks.com/topic/170684-if-mysql_num_rows-0-not-working/#findComment-900189 Share on other sites More sharing options...
Maq Posted August 17, 2009 Share Posted August 17, 2009 isnt it just if (mysql_num_rows($result) == 0) Not sure what the @ is doing The '@' symbol suppresses the error messages for that line. instead of die try exit - http://us.php.net/exit What is the point of that? They are equivalent. I took the @ out and it still displays the page and doesnt die...is it because im not specifically mentioning the ticker row in that statement??? sorry been trying to figure this out the whole weekend...still not working ahhh Are you sure it's not returning anything? Your code seems fine to me. Add an else statement with output and see if it displays. Quote Link to comment https://forums.phpfreaks.com/topic/170684-if-mysql_num_rows-0-not-working/#findComment-900194 Share on other sites More sharing options...
Mark Baker Posted August 17, 2009 Share Posted August 17, 2009 mysql_num_rows tells you the number of rows returned, not whether any specific column value for those rows is empty. You'd have to go through the returned result set and check the value of ticker for each row to determine that. The alternative is to modify your query, so that it only returns rows where the value of the ticker column isn't empty $result = mysql_query("SELECT location_id, ticker FROM host_locations WHERE location_id='2' AND ticker IS NOT NULL")or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/170684-if-mysql_num_rows-0-not-working/#findComment-900196 Share on other sites More sharing options...
shane85 Posted August 17, 2009 Author Share Posted August 17, 2009 im really sorry guys....still not working....below im posting the entire script for the page, except without the database connection details...it is as follows // make a connection to mysql here $conn = mysql_connect ($dbhost, $dbuser, $dbpass) or die ("I cannot connect to the database because: " . mysql_error()); mysql_select_db ($dbname) or die ("I cannot select the database '$dbname' because: " . mysql_error()); $result = mysql_query("SELECT location_id, ticker FROM host_locations WHERE location_id='2' AND ticker IS NOT NULL")or die(mysql_error()); //$result = mysql_query("SELECT location_id, ticker FROM host_locations WHERE location_id='2'")or die(mysql_error()); if(mysql_num_rows($result)==0){ exit('No data available'); } header('Content-type: application/xml'); echo "<rss version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom\">\n"; echo "<channel>\n"; echo "<title>RSS Feed</title>\n"; echo "<link>http://www.website.ca</link>\n"; while ($row = mysql_fetch_object($result)) { echo "<item>\n"; echo "<title>$row->ticker</title>\n"; echo "</item>\n"; } echo "</channel>\n"; echo "</rss>\n"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/170684-if-mysql_num_rows-0-not-working/#findComment-900200 Share on other sites More sharing options...
Mark Baker Posted August 17, 2009 Share Posted August 17, 2009 im really sorry guys....still not working....below im posting the entire script for the page, except without the database connection details...it is as followsIf ticker isn't NULL, then modify the query to ensure that it isn't an empty string either (assuming that the column is a VARCHAR type) AND ticker <> '' Quote Link to comment https://forums.phpfreaks.com/topic/170684-if-mysql_num_rows-0-not-working/#findComment-900207 Share on other sites More sharing options...
shane85 Posted August 17, 2009 Author Share Posted August 17, 2009 Mark - it is a varchar type...sorry for the newbie question but how would I do that?? Quote Link to comment https://forums.phpfreaks.com/topic/170684-if-mysql_num_rows-0-not-working/#findComment-900210 Share on other sites More sharing options...
Mark Baker Posted August 17, 2009 Share Posted August 17, 2009 Mark - it is a varchar type...sorry for the newbie question but how would I do that?? $result = mysql_query("SELECT location_id, ticker FROM host_locations WHERE location_id='2' AND ticker IS NOT NULL AND ticker <> ''")or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/170684-if-mysql_num_rows-0-not-working/#findComment-900214 Share on other sites More sharing options...
shane85 Posted August 17, 2009 Author Share Posted August 17, 2009 I used that code Mark and now the RSS page will not display...I get "Feed Code Error" Quote Link to comment https://forums.phpfreaks.com/topic/170684-if-mysql_num_rows-0-not-working/#findComment-900219 Share on other sites More sharing options...
shane85 Posted August 17, 2009 Author Share Posted August 17, 2009 bump! sorry! Quote Link to comment https://forums.phpfreaks.com/topic/170684-if-mysql_num_rows-0-not-working/#findComment-900257 Share on other sites More sharing options...
heldenbrau Posted August 17, 2009 Share Posted August 17, 2009 have you tried echoing $result to see if it is 0 and not null or something like that. Quote Link to comment https://forums.phpfreaks.com/topic/170684-if-mysql_num_rows-0-not-working/#findComment-900285 Share on other sites More sharing options...
Maq Posted August 17, 2009 Share Posted August 17, 2009 have you tried echoing $result to see if it is 0 and not null or something like that. $result is a resource id. Not what the OP is looking for. Quote Link to comment https://forums.phpfreaks.com/topic/170684-if-mysql_num_rows-0-not-working/#findComment-900291 Share on other sites More sharing options...
shane85 Posted August 17, 2009 Author Share Posted August 17, 2009 wow is this ever frustrating....I tried echoing result, and all it did was cause the page not to be displayed and say "feed code error"....what is wrong??? you will notice ive tried 3 different querys, I just commented out the ones im not using // make a connection to mysql here $conn = mysql_connect ($dbhost, $dbuser, $dbpass) or die ("I cannot connect to the database because: " . mysql_error()); mysql_select_db ($dbname) or die ("I cannot select the database '$dbname' because: " . mysql_error()); //$result = mysql_query("SELECT location_id, ticker FROM host_locations WHERE location_id='2' AND ticker IS NOT NULL AND ticker <> ''")or die(mysql_error()); $result = mysql_query("SELECT location_id, ticker FROM host_locations WHERE location_id='2' AND ticker IS NOT NULL")or die(mysql_error()); //$result = mysql_query("SELECT location_id, ticker FROM host_locations WHERE location_id='2'")or die(mysql_error()); echo "$result"; if(mysql_num_rows($result)==0){ exit('No data available'); } header('Content-type: application/xml'); echo "<rss version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom\">\n"; echo "<channel>\n"; echo "<title>RSS Feed</title>\n"; while ($row = mysql_fetch_object($result)) { echo "<item>\n"; echo "<title>$row->ticker</title>\n"; echo "</item>\n"; } /echo "</channel>\n"; echo "</rss>\n"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/170684-if-mysql_num_rows-0-not-working/#findComment-900353 Share on other sites More sharing options...
heldenbrau Posted August 17, 2009 Share Posted August 17, 2009 What I mean is $count = mysql_num_rows($result); echo $count see if it says 0 Quote Link to comment https://forums.phpfreaks.com/topic/170684-if-mysql_num_rows-0-not-working/#findComment-900474 Share on other sites More sharing options...
shane85 Posted August 18, 2009 Author Share Posted August 18, 2009 when I add that all it does is cause it to be feed code error, and not dispaly the page...here is the php again // make a connection to mysql here $conn = mysql_connect ($dbhost, $dbuser, $dbpass) or die ("I cannot connect to the database because: " . mysql_error()); mysql_select_db ($dbname) or die ("I cannot select the database '$dbname' because: " . mysql_error()); $result = mysql_query("SELECT location_id, ticker FROM host_locations WHERE location_id='2' AND ticker IS NOT NULL")or die(mysql_error()); //$result = mysql_query("SELECT location_id, ticker FROM host_locations WHERE location_id='2'")or die(mysql_error()); $count = mysql_num_rows($result); echo $count ; if(@mysql_num_rows($result)==0){ die('No data available'); } header('Content-type: application/xml'); echo "<rss version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom\">\n"; echo "<channel>\n"; echo "<title>RSS Feed</title>\n"; while ($row = mysql_fetch_object($result)) { echo "<item>\n"; echo "<title>$row->ticker</title>\n"; echo "</item>\n"; } echo "</channel>\n"; echo "</rss>\n"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/170684-if-mysql_num_rows-0-not-working/#findComment-900589 Share on other sites More sharing options...
shane85 Posted August 18, 2009 Author Share Posted August 18, 2009 I commented out all of the rss so it would echo $count, and whether "ticker" has text in it, or its empty, all $count displays is 1 Quote Link to comment https://forums.phpfreaks.com/topic/170684-if-mysql_num_rows-0-not-working/#findComment-900613 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.