Jump to content

Database Connection Issue


Daniel On The Web

Recommended Posts

<?php
$serv = "localhost:81";
$user = "root";
$password = "";


try {
	$con = new PDO("mysql:host=$serv;dbname=test", $user, $password);
	
	$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	
	echo "Successfully connected."; 
} catch (PDOException $e) {
	echo "Connection failed: " . $e->getMessage();
}
?>

Here I'm trying to use PDO to connect to my SQL database "test". However, I receive this error each time I connect:

 

SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it.

 

Am I connecting with the wrong server, user or password? Because I fail to see here the mistake.

Link to comment
Share on other sites

Your PHP code is correct. The issue most likely has something to do with your server setup.

 

I don't know your MySQL server's username, password or port number (and it wouldn't be smart to post the user and password on a public forum), but the issue could be that one of those is wrong.

Link to comment
Share on other sites

I don't know your MySQL server's username, password or port number (and it wouldn't be smart to post the user and password on a public forum), but the issue could be that one of those is wrong.

 

Of course. I know that I shouldn't post such kind of information.

 

However, I set up an user, a host and a password and wrote the same information in the PHP and the code is still throwing the error.

Edited by Daniel On The Web
Link to comment
Share on other sites

MYSQL DOES NOT USE SAME PORT AS WEB SERVER, so using localhost:80 WON'T work, default port for MYSQL database server is usually 3306, if you use just localhost, by default it will listen for port 3306 for MYSQLl server connection, using localhost:3306 will work also because it pointing to same port, but! localhost:80 WILL NOT!

Link to comment
Share on other sites

 

MYSQL DOES NOT USE SAME PORT AS WEB SERVER, so using localhost:80 WON'T work, default port for MYSQL database server is usually 3306, if you use just localhost, by default it will listen for port 3306 for MYSQLl server connection, using localhost:3306 will work also because it pointing to same port, but! localhost:80 WILL NOT!

Both "localhost" and "localhost:3306" didn't work as servers.

Link to comment
Share on other sites

I use...

$svr = "mysql:host=localhost;dbname=mydbname";

try {

    $pdo = new PDO($svr, $usr, $pwd);

Make sure you turned on the pdo mysql extension in php.ini.

 

Seems odd that you don't have a root password but I guess you've tested that in the mysql console.

Link to comment
Share on other sites

It's clear PDO's MySQL extension is active and enabled because it's attempting to connect to the MySQL server.

 

Perhaps you should install a server package like XAMPP on your computer for testing, it takes care of setting up the server properly for you. Installing Apache, MySQL and PHP separately is more prone to human error.

Link to comment
Share on other sites

I use...

$svr = "mysql:host=localhost;dbname=mydbname";

try {

    $pdo = new PDO($svr, $usr, $pwd);

Make sure you turned on the pdo mysql extension in php.ini.

 

Seems odd that you don't have a root password but I guess you've tested that in the mysql console.

Like this?: extension=php_pdo_mysql.dll Because that's how it appears on my php.ini. And it's not commented.

 

It's clear PDO's MySQL extension is active and enabled because it's attempting to connect to the MySQL server.

 

Perhaps you should install a server package like XAMPP on your computer for testing, it takes care of setting up the server properly for you. Installing Apache, MySQL and PHP separately is more prone to human error.

I'm already using it.

Link to comment
Share on other sites

Ignoring PDO for a moment, does mysqli work?

<?php
        
error_reporting(E_ALL);
ini_set('display_errors','On');
        
echo 'Php is running...';
echo '<br/>Connecting to MySQL... ';

$dbcon = mysqli_connect('localhost','root','');
$dbname = 'test';
        
if (!$dbcon){
    echo '<br/>Error: Unable to connect to MySQL.';
}else if(!mysqli_select_db($dbcon,$dbname)){
    echo '<br/>Error: Unable to open '.$dbname.' database.';
}else{
    echo '<br/>Connected to '.$dbname.' database';
}

?>

I have my Apache server running on port 8080 and mysql on the default, which is 3306 and I use the above (except I have a root password).

Link to comment
Share on other sites

Ignoring PDO for a moment, does mysqli work?

<?php
        
error_reporting(E_ALL);
ini_set('display_errors','On');
        
echo 'Php is running...';
echo '<br/>Connecting to MySQL... ';

$dbcon = mysqli_connect('localhost','root','');
$dbname = 'test';
        
if (!$dbcon){
    echo '<br/>Error: Unable to connect to MySQL.';
}else if(!mysqli_select_db($dbcon,$dbname)){
    echo '<br/>Error: Unable to open '.$dbname.' database.';
}else{
    echo '<br/>Connected to '.$dbname.' database';
}

?>

I have my Apache server running on port 8080 and mysql on the default, which is 3306 and I use the above (except I have a root password).

I tried with MySQLi but got the same error. Actually I don't care about using PDO or MySQLi. I'm choosing what works best.

Link to comment
Share on other sites

With MySQLi: No connection could be made because the target machine actively refused it.

With PDO: SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it.

 

So they are the same.

 

The console works well. The problem is connecting to the databases using PHP.

 

And I'm using a PC.

Edited by Daniel On The Web
Link to comment
Share on other sites

In phpmyadmin select test db ( make sure it is exactly as 'test' not 'Test' or 'test '), select permissions tab, and tell us what it says.

It appears to me as "Privileges", but I think it's the same.

 

The user I tried to use to connect to the database is:

User name: root

Host name: localhost

Type: global

Privileges: ALL PRIVILEGES

Grant: Yes

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...