simplifying code

Discussion in 'PHP' started by rojojat, Jul 30, 2009.

  1. #1
    Hi the code I have below works exactly the way I want it to. Im sure there is a way to simplify the if else statements, eliminating the

    {
    echo "";
    }

    part. Any suggestions? Thanks.













    $sql = "SELECT user_id FROM users WHERE email='$email'";
    $resulte = mysql_query($sql)
    or die("Couldn't execute QUERY.");
    $nume = mysql_num_rows($resulte);

    if(mysql_num_rows($resulte)==0)

    {
    echo "";

    }
    else
    {
    $mess = "The email you entered is not available.";
    include("new_register.php");
    exit();
    }




    \$sql = "SELECT user_id FROM users WHERE user='$user'";
    $resultu = mysql_query($sql)
    or die("Couldn't execute QUERY.");
    $numu = mysql_num_rows($resultu);

    if(mysql_num_rows($resultu)==0)

    {
    echo "";

    }
    else
    {
    $mess = "The Username you entered is not available, please try again.";
    include("new_register.php");
    exit();
    }



    $sql = "SELECT user_id FROM users WHERE pass1=SHA('$pass1')";
    $resultp = mysql_query($sql)
    or die("Couldn't execute QUERY.");
    $nump = mysql_num_rows($resultp);

    if(mysql_num_rows($resultp)==0) {
    $active=md5(uniqid(rand(), true));
     
    rojojat, Jul 30, 2009 IP
  2. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #2
    There is conditional statements known as ternary or whatever in php.

    echo $a == $b ? "true" : "false";

    If a is equal to b, it will print true, otherwise it will print false. IF it is if, elseif, elseif, else etc.. You will use a switch.
     
    Kaizoku, Jul 31, 2009 IP
  3. SHOwnsYou

    SHOwnsYou Peon

    Messages:
    209
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Instead of:

    
    if(mysql_num_rows($resultu)==0)
    
    {
    echo "";
    
    }
    else
    {
    $mess = "The Username you entered is not available, please try again.";
    include("new_register.php");
    exit();
    }
    
    PHP:
    Use:
    
    if(mysql_num_rows($resultu) > 0)
    {
    $mess = "The Username you entered is not available, please try again.";
    include("new_register.php");
    exit();
    }
    
    PHP:
     
    SHOwnsYou, Jul 31, 2009 IP
  4. ezprint2008

    ezprint2008 Well-Known Member

    Messages:
    611
    Likes Received:
    15
    Best Answers:
    2
    Trophy Points:
    140
    Digital Goods:
    1
    #4
    your code doesnt make any sense. Youre asking for a result to be returned from the database for email in the users table...and if 0 results come back (meaning theres no email that they just entered) your script is echoing " " (nothing..

    Then your else statement says -- otherwise if some result DOES come back .. tell them that they entered the wrong email or that it doesnt exist.
    This doesnt make any sense to me at all the way you have it set up.

    Usually if result == 0
    That means there's NO email in the database so then echo "No such email exists"
    ELSE = take them to the page or echo a logged in = "Hi ($username), welcome back to the website" blah blah.

    Your snippet leaves no way for somebody to successfully even log in etc.
     
    ezprint2008, Jul 31, 2009 IP
  5. rojojat

    rojojat Peon

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks Shownsyou, That's what I needed.

    to: ezprint
    I am still new to php. That is why I asked for help. I don't appreciate the way you have responded.
     
    Last edited: Jul 31, 2009
    rojojat, Jul 31, 2009 IP
  6. SHOwnsYou

    SHOwnsYou Peon

    Messages:
    209
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    ezprint -- It's not a login script - the script checks to make sure an email address and username are unique when someone tries to register.
     
    SHOwnsYou, Jul 31, 2009 IP