Jump to content

Unable To Connect To Mysql Database


scalar2

Recommended Posts

Hi,

 

i try to connect to my MySQL-Database with this PHP-Code:

 

<?php
if(isset($_GET["value"])){
	$value = $_GET["value"];
	$conn = new SQLConnection();
	$conn->insertData($value);
}

class SQLConnection{
	private $servername = "localhost";
	private $username = "";
	private $password = "";
	private $dbname = "";
	function __construct(){
	}
	
	function insertData($value){
		$conn = new mysqli($servername,$username,$password);
		if($conn->connect_errno)
			echo "Couldn't connect to database. " . $conn->errno . $conn->connect_errno;
		else{
			$conn->close();
		}
	}
}
?>
I created a second MySQL user explicitly for this database and i am 100% sure that i use the correct name / password.

 

But i always get:

Couldn't connect to database. 1045[/size]

I found this in the error log:

 

PHP Warning: mysqli::mysqli(): (HY000/1045): Access denied for user ''@'localhost' (using password: NO) in /home/scalar/public_html/file.php on line 17

PHP Warning: SQLConnection::insertData(): Couldn't fetch mysqli in /home/scalar/public_html/file.php on line 19

Link to comment
Share on other sites

The only reasons for #1045 are either the DB is not assigned to the DB user, or the username and password are wrong. If you're sure the password is right, make sure the username is in the form scalar_dbuser (all database users will start with your account name and a _ ), and make sure the DB has been assigned to the user.

Link to comment
Share on other sites

Thanks for your reply, i shortended my code a bit:

 

<?php
if(isset($_GET["value"])){
	$value = $_GET["value"];
	$conn = new SQLConnection();
	$conn->insertData($value);
}

class SQLConnection{
	public $servername = "localhost";
	public $username = "scalar_sql";
	public $password = "";
	public $dbname = "scalar_measurements";
	function __construct(){
	}
	
	function insertData($value){
		$link = mysqli_connect($servername,$username, $password, $dbname);
		
		if (!$link) {
			die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
		}
		echo 'Success... ' . mysqli_get_host_info($link) . "\n";
		
		mysqli_close($link);
	}
}
?>
As you can see here, i created a new user and assigned it to the database but i still get the same error, i don't know what i am doing wrong :(

 

In particular i don't understand why it is Access denied for user ''@'localhost'" , why doesn't the real username show up?

post-100473-0-47042500-1481569698_thumb.png

Link to comment
Share on other sites

I'm not completely sure, but it probably has something to do with the way you're calling your functions and the way you're assigning values to the variables. The way I troubleshoot something like this is to echo variables at various places in your code, and make small changes at a time. PHP errors tend to not be very informative.

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...