eugene
eugene

Reputation: 5

database connection error in php

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

Upvotes: 0

Views: 10072

Answers (4)

manip5595
manip5595

Reputation: 113

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());
 }

Upvotes: 0

Karthi
Karthi

Reputation: 528

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();
  }
?>

Upvotes: 1

Ruben Vincenten
Ruben Vincenten

Reputation: 907

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.

Upvotes: 1

Lulceltech
Lulceltech

Reputation: 1702

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.

Upvotes: 2

Related Questions