Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder stevenmw's Avatar
    Join Date
    Jun 2007
    Location
    OK
    Posts
    497
    Thanks
    27
    Thanked 31 Times in 31 Posts

    If Statement Gone Wrong But Looks Right?

    I'm working on a little scipt, and i can't figure out why my if statement is messing me up.

    PHP Code:
    <?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?

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,492
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    missing semicolon on the echo line?

    what do you mean by "gone wrong"?

  • #3
    Regular Coder stevenmw's Avatar
    Join Date
    Jun 2007
    Location
    OK
    Posts
    497
    Thanks
    27
    Thanked 31 Times in 31 Posts
    Sorry the semi colon was an over sight when I posted to the forums.

    My code is
    PHP Code:
    <?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.

  • #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,321
    Thanks
    60
    Thanked 525 Times in 512 Posts
    Blog Entries
    4
    From php.net:

    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:
    PHP Code:
    if (!mysql_num_rows($find))
    {
    //Your code

    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #5
    Regular Coder stevenmw's Avatar
    Join Date
    Jun 2007
    Location
    OK
    Posts
    497
    Thanks
    27
    Thanked 31 Times in 31 Posts
    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?

  • #6
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,321
    Thanks
    60
    Thanked 525 Times in 512 Posts
    Blog Entries
    4
    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.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #7
    Regular Coder stevenmw's Avatar
    Join Date
    Jun 2007
    Location
    OK
    Posts
    497
    Thanks
    27
    Thanked 31 Times in 31 Posts
    Quote Originally Posted by tangoforce View Post
    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)

  • #8
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,321
    Thanks
    60
    Thanked 525 Times in 512 Posts
    Blog Entries
    4
    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.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #9
    Regular Coder stevenmw's Avatar
    Join Date
    Jun 2007
    Location
    OK
    Posts
    497
    Thanks
    27
    Thanked 31 Times in 31 Posts
    That's what I was actually starting too think. I'll try it here in a bit. Thanks!

  • #10
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,321
    Thanks
    60
    Thanked 525 Times in 512 Posts
    Blog Entries
    4
    I reckon you're close to solving it now

    Right.. off to bed.. ZZZZzz
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •