0

Error checking login data Access denied for user 'username'@'localhost' (using password: NO)

My Databse Code

<?php
    $conn = mysql_connect('localhost', 'root','' );
    $db   = mysql_select_db('toys');
    if(!$db){
    echo mysqli_error($db);
    return;
    }  ?>

Error I get is user 'username'@'localhost' (using password: NO)

My welcome.php

<?php
include('1.php');
include_once('database.php'); 
include('sidebar.php');
$uname=$_SESSION['username'];
$s=mysql_query("select center from users where username='$uname'");
$df=mysql_fetch_array($s);
$_SESSION['center']=$df['center'];
//$_SESSION['vid']=$_REQUEST['id'];
?>

My user.php

<? ob_start(); ?>
<?php
if (isset($_GET['msg']))
$msg = $_GET['msg'];
$msg="User Login Here";
if(isset($_POST['login']) && $_POST['login']!=''){

  include_once('database.php');  
    $username = $_POST['username'];
    $pass = $_POST['pass'];
    $cent  =$_POST['center'];
  if($username=='')
  {
    $msg.= '<br>Please enter username';
  }
  if($pass=='')
  {
    $msg.= '<br>Please enter password';
  }
  if($cent=='')
  {
    $msg.= '<br>Please enter center';
  }
  if($username!='' && $pass!='' && $cent!='')
  {

    $username = mysql_real_escape_string( $username );
      $password = mysql_real_escape_string( md5($pass) );
      $center = mysql_real_escape_string($cent);
    $res = mysql_query("SELECT * FROM users WHERE (username='$username' and password='$password' and center='$center' ) ");

    if(!$res){
    echo 'Error checking login data <br/>' .mysql_error();
    return;
    }
    var_dump($row = mysql_fetch_row($res));
// exit();
      if( $row > 0 ) {

      session_start();
      $_SESSION['username'] = $username;
      header('location:home.php');
    }
    else{
      $msg=  "<h1>Invalid username or password</h1>";
    }
?> 

One more Question WHY it is working fine on wamp

Once I put it online it show that error. I tried everything but no change. my user.php shows up with login credentials but after login instead of moving too home .php it comes back to user.php with css but no details but a blank page with error

checked everything but no answer

4
  • what do you mean by 'tried everything' did you update your username and password on the live server? Commented Aug 24, 2016 at 7:55
  • And you should definitely stop using mysql and take a look at mysqli. Commented Aug 24, 2016 at 7:55
  • 3
    Stop using deprecated mysql_* API. Use mysqli_* or PDO Commented Aug 24, 2016 at 7:55
  • mysql_connect('localhost', 'root','' ) // this will not work in online server Commented Aug 24, 2016 at 8:21

4 Answers 4

2

Couple things: Your MySQL server username OR password is incorrect. Make sure you're using mysqli instead of mysql. You might have no root access on the live server.

Typically when you set up with a webhost they'll give you a sql username and password to use. Check the hosting email. If you set up a linux server yourself however you might have configured it wrong. Comment below if you're still having issues.

Sign up to request clarification or add additional context in comments.

8 Comments

When you say you "put it online" what do you mean by that? Are you hosting it with a company like hostgator? Or are you doing something different?
You're gonna have to share your home.php and login.php chunks of code, i'll take a peak and see if I can see your issue.
first change include and include_once to require_once;
Now we're gonna do some debugging. Back up your user.php file so you have a copy of it and change the current one to: <?php ob_start(); if (isset($_GET['msg'])) $msg = $_GET['msg']; $msg = "User Login Here"; if(isset($_POST['login']) && $_POST['login']!='') { require_once('database.php'); echo 'Entered if'; } Tell me what the output is.
Thought so, your issue is you're not setting your value login. In your script you're checking if you posted data to a value called login from a form. If you didn't nothing will show. So if you take out that if check it should work fine: <?php ob_start(); if (isset($_GET['msg'])) $msg = $_GET['msg']; $msg = "User Login Here"; require_once('database.php'); echo "now i'll show data";
|
1

You are mixing mysqli and mysql here, like suggested, use mysqli_* or PDO. To quickly fix your code:

<?php
$conn = mysql_connect('localhost', 'root','' );
if(!$conn) {
    die('can not connect:'. mysql_error());
}
if(!mysql_select_db('toys', $conn)) {
    die('cannot select db: '.mysql_error());
}
?>

As for the error regarding not being able to connect, check your mysql user credentials.

Comments

1

You can use any one either mysql or mysqli. above your code you did both in single file. Recommended one mysqli herei.

<?php
$con = mysqli_connect("localhost","my_user","my_password","my_db");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
?>

3 Comments

you get same problem.? cau you update your question with what you have latest tried
actually what is your problem plz be explain connectivity or login page ?
thats why i said you MUST update your question with code.
0

Check this link : https://www.w3schools.com/php/func_mysqli_connect_error.asp

$con=mysqli_connect("localhost","wrong_user","my_password","my_db");
// Check connection
if (!$con)
{
 die("Connection error: " . mysqli_connect_error());
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.