sql query help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
angelic_devil
Forum Commoner
Posts: 74
Joined: Thu Apr 02, 2009 7:05 am

sql query help

Post 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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: sql query help

Post 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.
angelic_devil
Forum Commoner
Posts: 74
Joined: Thu Apr 02, 2009 7:05 am

Re: sql query help

Post 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'" );
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: sql query help

Post 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.
angelic_devil
Forum Commoner
Posts: 74
Joined: Thu Apr 02, 2009 7:05 am

Re: sql query help

Post 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 = "";
                                
            }
            
        }  
angelic_devil
Forum Commoner
Posts: 74
Joined: Thu Apr 02, 2009 7:05 am

Re: sql query help

Post by angelic_devil »

Code: Select all

function confirm_query($result_set) {
        if (!$result_set) {
            die("Database query failed: " . mysql_error());
        }
 
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: sql query help

Post by RobertGonzalez »

Output your query and post it here so we can see it.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: sql query help

Post by s.dot »

$query .= "WHERE username = '{$username}' ";
$query .= "hashed_password = '{$hashed_password}' ";
Missing "AND".
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
angelic_devil
Forum Commoner
Posts: 74
Joined: Thu Apr 02, 2009 7:05 am

Re: sql query help

Post by angelic_devil »

data base query failed
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: sql query help

Post by RobertGonzalez »

What does mysql_error() tell you?
Post Reply