----Query was empty---
at
---$sql="SELECT * FROM users WHERE username='$username' AND password='$password'";
    $result=@mysql_query($sql) or die(mysql_error()); ---


<html>
<body>
<form action="login.php" method="post">
<div>
<table width="100%">
<tr>
<td><img src="Logofinalcopy.gif"></td>
</tr>
<tr>
<td bgcolor="aqua"><h2>Login</h2></td>
</tr></table>
<table align="right" style="width:40%">
<br>
<tr>
<td>username:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>password:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td align="CENTER" COLSPAN="4">
<input TYPE="SUBMIT" name="submit" value="Login">
<input TYPE="reset" name="submit" value="clear"></td>
</tr>
</table>
</div>
</form>
</body>
</html>
<?php
if(isset($_POST['submit']))
{
 $con = @mysql_connect("10.70.1.50","invensis","invensis");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
 $con_db=@mysql_select_db("database_myproject",$con);
if (!$con)
  {
  die('Could not connect DB: ' . mysql_error());
  }

  $username=$_POST['username'];
  $password=$_POST['password'];

  $username = stripslashes($username);
  $password = stripslashes($password);
  $username = mysql_real_escape_string($username);
  $password = mysql_real_escape_string($password);

  $flag="OK";  
  $msg=""; 
     
 if(strlen($username) < 1)
   {
          $msg=$msg."Please enter the username<br>";
          $flag="NOTOK"; 
   }
 if(strlen($password) < 1)
   {
        $msg=$msg."Please enter the password<br>";
        $flag="NOTOK";  
   }
  if($flag <>"OK")
  {
  echo "<strong style='color:#FF3333'>"."<left >Please enter Username or Password  </left>"."</strong>";
  }
  else
    {
    $sql="SELECT * FROM users WHERE username='$username' AND password='$password'";
    $result=@mysql_query($sql) or die(mysql_error()); 

    $count= mysql_num_rows($result);
    echo "$count";
    if($count==1)
       {
            $sql = mysql_query("SELECT role FROM users WHERE username='$username' AND password='$password'");
            while($info = @mysql_fetch_array($sql))
            {
                if($info['role']=='Super Admin')
                {
                    @header('location: [url]http://localhost/Project/Superadmin.php');[/url]
                } 
                else if($info['role']=='Admin')
                {
                    @header('location: [url]http://localhost/Project/Admin.php');[/url]
                } 
                else if($info['role']=='User')
                {
                    @header("location: http://localhost/Project/User.php");
                } 
            }    
        
        } 
        else 
        {
            echo "<strong style='color:#FF3333'>Incorrect User Name OR Password</strong>";
        }
      
}
}
?>

Dani AI

Generated

Quick diagnostic and what to try first — the MySQL message "Query was empty" means PHP called mysql_query() with an empty string (no SQL to run). That usually happens because the SQL variable was never built, was overwritten, or an earlier error stopped the code that builds it. Printing the built SQL and the input variables is the fastest way to see that. 's suggestion to show the query was on the right track. mysql_query() manual. (php.net)

Concrete, ordered debugging steps to follow now:

  • Stop hiding errors: remove the @ operators and enable full error reporting so PHP shows warnings/notices (these often point to the real cause).
  • After you build the SQL string, dump the values to confirm they are what you expect and stop execution so you can inspect them.
  • Verify the database was actually selected — mysql_select_db() returns true/false and you should check that result before calling mysql_query().

Example quick checks (add them temporarily, then remove once fixed):

error_reporting(E_ALL);
ini_set('display_errors', 1);

var_dump($sql, $username, $password);
die;

if (!$con_db) {
    die('DB select failed: ' . mysql_error());
}

See PHP error reporting and mysql_select_db docs for details. (php.net)

Header/redirect notes (these will bite you once query logic is fixed): header() must be sent before any HTML output, and the redirect should use the Location: header and then exit to stop the script. Also remove any forum/BBCode markup that accidentally made it into your header strings — that will break PHP. Example:

header('Location: /Project/Superadmin.php');
exit;

See the header() docs for the details. (php.net)

Security and longer-term fixes — do not keep plaintext passwords and do not rely on ext/mysql in new code. Move to prepared statements (mysqli or PDO) and use password_hash()/password_verify() for passwords; that both fixes security and avoids many subtle bugs. For a modern rewrite see the mysqli/PDO docs and password_hash() docs. 's quote-style suggestion can work syntactically, but it won't fix an empty $sql or suppressed errors — debugging the steps above will show the real cause. (php.net)

Recommended Answers

All 3 Replies

hi

Your query will come like

"$sql="SELECT * FROM users WHERE username='".$username."' AND password='".$password."'";

Please try with this one and let me know if still any problem comes

Hiiee

Use This Query

$sql="SELECT * FROM users WHERE username="'.$username.'" AND password="'.$password.'" ";

InStead Of This

$sql="SELECT * FROM users WHERE username='$username' AND password='$password'";

Check & Let Me Know If U Find Anything Else.

Do It For All Queries In Ur File. That Will Work.

Thanks.
ScmSimplyBest

Do one thing ,

echo you $sql variable in your script then write die(); so when you run code you will see that query only.

Now take that query and run it in PHPMYADMIN and check is it right or your get result from same ??

may be you can get some idea wheres the problem is ?

Best luck..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.