[SOLVED] Somebody Help me!
Moderator: General Moderators
-
michael_lihai
- Forum Newbie
- Posts: 6
- Joined: Sun Mar 14, 2004 2:32 pm
[SOLVED] Somebody Help me!
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.
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.
-
michael_lihai
- Forum Newbie
- Posts: 6
- Joined: Sun Mar 14, 2004 2:32 pm
-
TheBentinel.com
- Forum Contributor
- Posts: 282
- Joined: Wed Mar 10, 2004 1:52 pm
- Location: Columbus, Ohio
Re: Somebody Help me!
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:michael_lihai wrote:Code: Select all
$result=mysql_query($sql, $db); if ($result) { ...
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.)
-
michael_lihai
- Forum Newbie
- Posts: 6
- Joined: Sun Mar 14, 2004 2:32 pm
-
TheBentinel.com
- Forum Contributor
- Posts: 282
- Joined: Wed Mar 10, 2004 1:52 pm
- Location: Columbus, Ohio
-
michael_lihai
- Forum Newbie
- Posts: 6
- Joined: Sun Mar 14, 2004 2:32 pm
-
Illusionist
- Forum Regular
- Posts: 903
- Joined: Mon Jan 12, 2004 9:32 pm
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.";
}-
Illusionist
- Forum Regular
- Posts: 903
- Joined: Mon Jan 12, 2004 9:32 pm
-
michael_lihai
- Forum Newbie
- Posts: 6
- Joined: Sun Mar 14, 2004 2:32 pm