...

View Full Version : If Statement Gone Wrong But Looks Right?



stevenmw
03-01-2012, 12:19 AM
I'm working on a little scipt, and i can't figure out why my if statement is messing me up.



<?php
$host="---";
$username="---";
$password="---";
$db_name="---";
$tbl_name="---";

mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");


$user = $_POST['username'];
$pass = $_POST['password'];

$find = mysql_query("SELECT * FROM bb_users WHERE username_clean='$user'");

if (mysql_num_rows($find)==0) {
echo "username not found"
}




?>

Can anyone see where I'm going wrong?

mlseim
03-01-2012, 12:24 AM
missing semicolon on the echo line?

what do you mean by "gone wrong"?

stevenmw
03-01-2012, 12:38 AM
Sorry the semi colon was an over sight when I posted to the forums.

My code is


<?php
$host="basicwebcorecom.ipagemysql.com";
$username="bwc";
$password="turtle";
$db_name="pbb";
$tbl_name="bb_users";

mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");


$user = $_POST['username'];
$pass = $_POST['password'];

$find = mysql_query("SELECT * FROM bb_users WHERE username_clean='$user'");

if (mysql_num_rows($find)==0) {
echo "username not found";
}




?>


By gone wrong I mean that the text echos no matter what. Should I add an "isset" before some where?


I don't get any errors. The text just echos on it's own.

tangoforce
03-01-2012, 12:43 AM
From php.net (http://uk.php.net/mysql_num_rows):



Return Values

The number of rows in a result set on success or FALSE on failure.


Note the false. Therefore you need to use === instead of ==.

Also you could do this:


if (!mysql_num_rows($find))
{
//Your code
}

stevenmw
03-01-2012, 12:46 AM
My problem is that when I load my php file the message that is supposed to echo if there are 0 matching results in the forum display automatically. And this message stays there. Causing the if statement not even to function.

Any ideas?

tangoforce
03-01-2012, 12:51 AM
Have you even tried my suggestion yet? - That was such a quick reply I have to ask.

Failing that, you have an error in your SQL which is causing mysql to return 0 results.

The message can't 'stay there' and then cause the if function not to run, the if function HAS run and has evaluated to true.

stevenmw
03-01-2012, 12:52 AM
Have you even tried my suggestion yet? - That was such a quick reply I have to ask.

Failing that, you have an error in your SQL which is causing mysql to return 0 results.


I tried to negate it like you said, but it didn't work. (I replied quick becausei had all of the stuff pulled up so it was a quick check)

tangoforce
03-01-2012, 12:54 AM
In that case, run the query through phpmyadmin and see what happens (remember to replace the variable though).

That said, are you sure $user is actually set? - IE not empty? - You've done no checking on the $_POST field to be sure it exists.

stevenmw
03-01-2012, 12:56 AM
That's what I was actually starting too think. I'll try it here in a bit. Thanks!

tangoforce
03-01-2012, 12:58 AM
I reckon you're close to solving it now :thumbsup:

Right.. off to bed.. ZZZZzz



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum