Jump to content

Archived

This topic is now archived and is closed to further replies.

shane85

IF mysql_num_rows == 0 ... not working

Recommended Posts

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?

Share this post


Link to post
Share on other sites

isnt it just

 

if (mysql_num_rows($result) == 0)

 

Not sure what the @ is doing

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

tried...oof..same thing happens...I dont see why it isnt exiting the page?? doesnt make sence??

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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());

Share this post


Link to post
Share on other sites

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";

 

?>

Share this post


Link to post
Share on other sites
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
If 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 <> ''

Share this post


Link to post
Share on other sites

Mark - it is a varchar type...sorry for the newbie question but how would I do that??

Share this post


Link to post
Share on other sites

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());

Share this post


Link to post
Share on other sites

I used that code Mark and now the RSS page will not display...I get "Feed Code Error"

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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";

 

?>

Share this post


Link to post
Share on other sites

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";

 

?>

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.