Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2002
    Posts
    31

    Unanswered: Help with PHP and DB query

    Hello,

    I am attempting to finish up an authentication piece to a web app I am writing. When the app's administrator attempts to add a new user I want the php code to check the database to see if the user name is all ready there. If it is I want to through back an error saying the user name is all ready taken. I have writen the code and I'm pretty sure my SQL query is correct because it returns the correct result when I run it from mysql, but php isn't doing what I'm wanting it to do

    CODE BELOW:

    //Var declarations
    $user_name = $_POST["txt_UserName"];
    $pass_word = $_POST["txt_PassWord"];
    $pass_conf = $_POST["txt_PassConf"];
    $priv_level = $_POST["sel_priv"];
    $is_active = "Y";
    $add_user = "T";
    $cryp_pass = crypt($pass_word, 'sl');

    //Code block
    $sql = "SELECT UserID FROM auth WHERE UserName = '$user_name'";
    $result = mysqli_query($db, $sql);
    if($result == $user_name)
    {
    echo "<p>We are sorry that user name is all ready taken. Please try again";
    echo "<meta http-equiv='refresh' content='3;URL=add_user_form.php'>";
    }

    -----I also tried this -----

    $sql = "SELECT UserID FROM auth WHERE UserName = '$user_name'";
    $result = mysqli_query($db, $sql);
    if($result > 0)
    {
    echo "<p>We are sorry that user name is all ready taken. Please try again";
    echo "<meta http-equiv='refresh' content='3;URL=add_user_form.php'>";
    }

    I really don't understand what I'm doing wrong here, below is a description of my DB table.

    mysql> describe auth;
    +-----------+-----------+------+-----+---------+----------------+
    | Field | Type | Null | Key | Default | Extra |
    +-----------+-----------+------+-----+---------+----------------+
    | UserID | int(11) | NO | PRI | NULL | auto_increment |
    | UserName | char(16) | NO | | | |
    | PassWord | char(120) | NO | | | |
    | IsActive | char(1) | YES | | NULL | |
    | PrivLevel | int(1) | YES | | NULL | |
    +-----------+-----------+------+-----+---------+----------------+

    Can any one please help me here?

    Thank you,
    saiello

  2. #2
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    I'm sure we have already answered this query somewhere else for you. However in the event we haven't :

    What you are doing wrong is essentially with your understand of the logic of mysqli_query. It produces a dataset which you have to utilise using the mysqli function groups. Thus -

    Code:
    $sql = "SELECT UserID FROM auth WHERE UserName = '$user_name'";
    $result = mysqli_query($db, $sql);
    while($row = mysqli_fetch_assoc($result)
    {
      if($row['UserID'] > 0) {echo "I am a real user";} 
    }
    Or

    Code:
    $sql = "SELECT UserID FROM auth WHERE UserName = '$user_name'";
    $result = mysqli_query($db, $sql);
    if(mysqli_num_rows($result) > 0)
    {
     echo "i am authenticated";
    }

Posting Permissions

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