Page 1 of 1
[SOLVED] Somebody Help me!
Posted: Sun Mar 14, 2004 2:32 pm
by michael_lihai
I can not find what's wrong with the following script, it always find mached record even there is no mached record in table 'tb'.
Script
$id="1234567";
$pname="test";
$sql="select * from tb where sid='$id' and poname='$pname'";
$result=mysql_query($sql, $db);
if ($result)
{
echo "find.";
}else{
echo "can not find.";
}
Please be kind to help me.
Thank you very much.
Posted: Sun Mar 14, 2004 2:41 pm
by tim
try adding some: or die (mysql_error()); in your sql commands and see if that tells you anything?
Posted: Sun Mar 14, 2004 2:49 pm
by tim
also
what do you get if you echo the result var?
Posted: Sun Mar 14, 2004 2:50 pm
by michael_lihai
I have set or die mysql_error(); with connecting database, it works well for other SQL queries, except only this script segment.
Re: Somebody Help me!
Posted: Sun Mar 14, 2004 2:52 pm
by TheBentinel.com
michael_lihai wrote:
Code: Select all
$result=mysql_query($sql, $db);
if ($result)
{ ...
I'm not sure that testing $result in this way is telling you what you want to know. The manual page for mysql_query says:
Only for SELECT,SHOW,EXPLAIN or DESCRIBE statements mysql_query() returns a resource identifier or FALSE if the query was not executed correctly. For other type of SQL statements, mysql_query() returns TRUE on success and FALSE on error. A non-FALSE return value means that the query was legal and could be executed by the server. It does not indicate anything about the number of rows affected or returned. It is perfectly possible for a query to succeed but affect no rows or return no rows.
So I think it might evaluate to true even if no records were returned. Is there another way to check it, like number of rows returned, or something like that? (I don't know all the mysql_* stuff, but there's probably a function for it.)
Posted: Sun Mar 14, 2004 2:52 pm
by michael_lihai
when I echo the result var, it shows nothing.
Posted: Sun Mar 14, 2004 3:02 pm
by TheBentinel.com
michael_lihai wrote:when I echo the result var, it shows nothing.
What if you echo mysql_num_rows($result)? If that's 0, then you didn't get any rows back.
Posted: Sun Mar 14, 2004 3:13 pm
by michael_lihai
it does result 0
Posted: Sun Mar 14, 2004 3:16 pm
by michael_lihai
does it mean "select ....." has problem here, then what's the problem?
Posted: Sun Mar 14, 2004 3:22 pm
by Illusionist
no there is nothing wrong with your select statment. thebentinel was trying to say, that using the if statement like that is not the best way to do it. Because the query could succed, or go through with no error, and still not find anything, and it would put something in $result. The best way to do it would be:
Code: Select all
$id="1234567";
$pname="test";
$sql="select * from tb where sid='$id' and poname='$pname'";
$result=mysql_query($sql, $db);
if (mysql_num_rows($result)>0)
{
echo "find.";
}else{
echo "can not find.";
}
Posted: Sun Mar 14, 2004 3:24 pm
by tim
if (mysql_numrows($result)>0)
change
if (mysql_num_rows($result)>0)
that would be the best way I can think of too
Posted: Sun Mar 14, 2004 3:27 pm
by Illusionist
they are the same thing...
Posted: Sun Mar 14, 2004 3:27 pm
by michael_lihai
You are absolutely correct. when I changed as what you said, it works well.
Thank you very much for your Strong Help.
Now, I know how to avoid this thing happening later.
Thanks a lot.