[SOLVED] Somebody Help me!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
michael_lihai
Forum Newbie
Posts: 6
Joined: Sun Mar 14, 2004 2:32 pm

[SOLVED] Somebody Help me!

Post 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.
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

try adding some: or die (mysql_error()); in your sql commands and see if that tells you anything?
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

also

what do you get if you echo the result var?
michael_lihai
Forum Newbie
Posts: 6
Joined: Sun Mar 14, 2004 2:32 pm

Post 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.
TheBentinel.com
Forum Contributor
Posts: 282
Joined: Wed Mar 10, 2004 1:52 pm
Location: Columbus, Ohio

Re: Somebody Help me!

Post 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.)
michael_lihai
Forum Newbie
Posts: 6
Joined: Sun Mar 14, 2004 2:32 pm

Post by michael_lihai »

when I echo the result var, it shows nothing.
TheBentinel.com
Forum Contributor
Posts: 282
Joined: Wed Mar 10, 2004 1:52 pm
Location: Columbus, Ohio

Post 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.
michael_lihai
Forum Newbie
Posts: 6
Joined: Sun Mar 14, 2004 2:32 pm

Post by michael_lihai »

it does result 0
michael_lihai
Forum Newbie
Posts: 6
Joined: Sun Mar 14, 2004 2:32 pm

Post by michael_lihai »

does it mean "select ....." has problem here, then what's the problem?
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post 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.";
}
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post 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
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

they are the same thing...
michael_lihai
Forum Newbie
Posts: 6
Joined: Sun Mar 14, 2004 2:32 pm

Post 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.
Post Reply