Jump to content

IF mysql_num_rows == 0 ... not working


shane85

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?

Link to comment
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.

Link to comment
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());

Link to comment
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";

 

?>

Link to comment
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 <> ''

Link to comment
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";

 

?>

Link to comment
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";

 

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.