0
votes

I have a script and when I go to the website link(http://kodle.co.uk/login.php) the script comes back with this:

Warning: mysql_connect(): Access denied for user 'root'@'localhost' (using >password: NO) in /home/hkode/public_html/dbconnect.php on line 12

Warning: mysql_select_db(): Access denied for user 'hkode'@'localhost' (using >password: NO) in /home/hkode/public_html/dbconnect.php on line 13

Warning: mysql_select_db(): A link to the server could not be established in >/home/hkode/public_html/dbconnect.php on line 13 Connection failed : Access denied for user 'hkode'@'localhost' (using password: >NO)

The DBCONNECT.php script is as follows:

<?php

// this will avoid mysql_connect() deprecation error.
error_reporting( ~E_DEPRECATED & ~E_NOTICE );
// but I strongly suggest you to use PDO or MySQLi.

define('DBHOST', 'localhost');
define('DBUSER', 'root');
define('DBPASS', '');
define('DBNAME', 'login_base');

$conn = mysql_connect("localhost", "root", "");
$dbcon = mysql_select_db(DBNAME);

if ( !$conn ) {
    die("Connection failed : " . mysql_error());
}

if ( !$dbcon ) {
    die("Database Connection failed : " . mysql_error());
}
5
there is no password setBernd Buffen
You create a connection $conn...but then select a db on $dbcon (which I don't see set anywhere). That doesn't look like it'd work.John Corry

5 Answers

0
votes

Go to your database and check if user root actually does not require a password. If it does then insert it as a third parameter in the empty single quotes below. Now use the code below and you will be just fine. Happy new year.

<?php
$con = mysql_connect('REPLACE_THIS_WITH_IP_ADDRESS_OF_YOUR_SERVER', 'root', ''); 
if (!$con) { 
die('Could not connect: ' . mysql_error()); 
} 
mysql_select_db("login_base", $con); 
?>
0
votes
define('DBPASS', '');

You want to replace '' with 'youractualpassword'

Then change

mysql_connect("localhost", "root", "");

to

 mysql_connect(DBHOST, DBUSER, DBPASS);

On a further note the comment

// this will avoid mysql_connect() deprecation error.
error_reporting( ~E_DEPRECATED & ~E_NOTICE );
// but I strongly suggest you to use PDO or MySQLi.

Suggest you don't use these methods. You should only do this if you absolutely know what you are doing, which seems like you do not.

I highly suggest using something like laracasts to learn php. Good luck

0
votes

It seems like the password is not right for user root also i would suggest you to use the constants for connecting to mysql which u have already defined. i would rewrite connection statement and subsequent statement something like this

$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
    if ( !$conn ) { 
    die("Connection failed : " . mysql_error());
}else{ // connection succesful
   $dbcon = mysql_select_db(DBNAME);
}
0
votes

Try this

$conn = mysql_connect(DBHOST, DBUSER);
$dbcon = mysql_select_db(DBNAME, $conn);

Edited Check all passwords

0
votes

mysql is deprecated you have to use mysqli or pdo. example of mysqli is ...

 $hostname= "localhost";
 $username = "root";
 $password = "";
 $dbname = "your_database_name";
 $con = new mysqli($hostname, $username, $password,$dbname);

 if ($con->connect_error) {
 die("Connection failed: " . $con->connect_error);
 } 
 $sql = "your_query";

 if ($con->query($sql) === TRUE) {
   echo "On query result is successfull";
 } else {
   echo "Error: " . $sql . "<br>" . $con->error;
}
$con->close();