Forum Moderators: coopster

Message Too Old, No Replies

Correct method if no data found

Calling rows from mysql, if they are empty or not found how to answer

         

wheelie34

2:19 pm on May 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have a db with content but as google caches the pages created what happens if one of the pages content has been deleted.

If the call to the page has no $num key I send them to the front of the site with a redirect, now after doing site: in google I noticed it has a page that no longer has content from the db so I tried another header location but got the "headers already sent warning" heres my code

$sql = "select * from db where num ='$num'";

if ($_REQUEST[num] == '' ¦¦ mysql_num_rows($sql < 1))
{
header("Location: http://www.example.com/");
exit;
}

I now get the following message if a page HAS content and is displayed

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource

Is the above correct? what do I need to do about the Warning?

Thanks in advance

EDIT sorry mod's I should have posted this in the database forum, please move if you feel fit

wheelie34

3:48 pm on May 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



ok no replies so I continued fiddling, heres where I got

$sql = "select * from db where num ='$num'";
$result = mysql_query($sql);
$no_result = mysql_num_rows($result);

if ($_REQUEST[num] == '' ¦¦ $no_result < 1)
{
header("Location: http://www.example.com/");
exit;
}

Does the above look ok

[edited by: eelixduppy at 4:36 pm (utc) on May 17, 2007]
[edit reason] delinked [/edit]

eelixduppy

4:47 pm on May 17, 2007 (gmt 0)



The above looks good and you have addressed the problem that you were having: not actually sending the query to the mysql server using mysql_query.

There is another problem, however, that may not actually be a problem but I don't know what you have before this code. Just note that having

$num
in the query, if the variable's value is defined by the user (or can be), can present a security issue through SQL Injection. Read up on mysql_real_escape_string [php.net] for more information on what can be done to prevent this.

>>I should have posted this in the database forum

No worries. This is a PHP related problem and should be in here :)

wheelie34

5:13 pm on May 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks eelixduppy