Page 1 of 1

sql query help

Posted: Thu Apr 02, 2009 4:02 pm
by angelic_devil
can some one help me with this

i need a sql code for retrieving rows with all following condition

1. username =albert
2.password="$password"
3. status="A"

plz suggest a code that retrieves the records with all 3 above conditions.

thanx

Re: sql query help

Posted: Thu Apr 02, 2009 4:38 pm
by RobertGonzalez
Have you tried anything? Because it sounds like you want to SELECT all rows (I think it is * ;) ) FROM whatever table has the data WHERE username = 'albert' AND password='$password' AND status='A'.

Remember too that when building SQL queries for MySQL that it is always best to wrap database names, table names and column names in backticks in your query.

Re: sql query help

Posted: Thu Apr 02, 2009 5:39 pm
by angelic_devil
is this correct?

$result_set = mysql_query("SELECT username,status,hashed_folderid FROM users WHERE username = '{$username}' AND hashed_password = '{$hashed_password}' AND status = 'A'" );

Re: sql query help

Posted: Fri Apr 03, 2009 12:55 pm
by RobertGonzalez
What does the database tell you? It is ok to try it and see if it works. In fact, it is encouraged that you do that. If it doesn't work, let us know so we can help you further.

Re: sql query help

Posted: Sat Apr 04, 2009 3:17 pm
by angelic_devil
nope its not working i want it to check the status in the table users and based on that it shud allow the entry.

i m getting the error database query failed for login.php

Code: Select all

 
        
        if ( empty($errors) ) 
        {
        
            // Check database to see if username and the hashed password exist there.
            $query = "SELECT username ";
            $query .= "FROM users ";
            $query .= "WHERE username = '{$username}' ";
            $query .= "hashed_password = '{$hashed_password}' ";
            $query .= "LIMIT 1";
            $result_set = mysql_query($query);
            confirm_query($result_set);
            if (mysql_num_rows($result_set) == 1)
            {
                // username/password authenticated
                // and only 1 match
                $status_query = "SELECT status from users WHERE username = '{$username}'" ;
                $status_field_query = mysql_fetch_assoc($status_query);
                switch ($status_field_query)
                    {
                      case "P":
                               $message = " Sorry your are still not approved by the admin ";
                               break;
                      case "B":
                               $message = " Sorry your are Banned from this site by the admin ";
                               break;
                      case "S":
                               $message = " Sorry your are suspended by the admin ";
                               break;
                      default :         
                            
                        $_SESSION['username'] = $found_user['username'];
                        redirect_to("welcome.php");
                    }
            }
            
                    
            else 
            {
                // username/password combo was not found in the database
                $message = "Username/password combination incorrect.<br />
                    Please make sure your caps lock key is off and try again.";
                    $username = "";
                    $password = "";
                                
            }
            
        }  

Re: sql query help

Posted: Sat Apr 04, 2009 3:32 pm
by angelic_devil

Code: Select all

function confirm_query($result_set) {
        if (!$result_set) {
            die("Database query failed: " . mysql_error());
        }
 

Re: sql query help

Posted: Sat Apr 04, 2009 7:42 pm
by RobertGonzalez
Output your query and post it here so we can see it.

Re: sql query help

Posted: Sat Apr 04, 2009 8:01 pm
by s.dot
$query .= "WHERE username = '{$username}' ";
$query .= "hashed_password = '{$hashed_password}' ";
Missing "AND".

Re: sql query help

Posted: Sun Apr 05, 2009 2:06 pm
by angelic_devil
data base query failed

Re: sql query help

Posted: Sun Apr 05, 2009 2:28 pm
by RobertGonzalez
What does mysql_error() tell you?