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

    Unanswered: Help with a MySQL query in php

    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
    Your php logic is wrong i'm afraid :

    $result = mysqli_query($db,$sql) returns a resultset stream. You need to use mysqli_fetchassoc or mysqli_fetcharray or another equivalent to get the data you're after.

    What you really want is :

    if(mysqli_num_rows($result) > 0) {
    echo "error";
    }
    Last edited by aschk; 03-26-07 at 09:17.

  3. #3
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    as a side note : I would make your IsActive field an ENUM type with ENUM('active','inactive') . Just a preference and certainly not something to adhere to. If you choose not to, make it a BOOLEAN or INT unless there is a particular reason for CHAR(1).

  4. #4
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    CHAR(1) is portable, as is TINYINT (although it might translate to SMALLINT)

    but the dreaded, evil ENUM will never work in any other database system
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

Posting Permissions

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