Question: I am trying to connect my PHP code to MySQL safely with the following code:

```
<html>
    <?php
        $con = mysql_connect("localhost:3306","root","password");
        if(!$con)
        {
            die('Could not connect: ' . mysql_error());
        }
        else
        {
            echo "Connection established!";
        }
        mysql_close($con);
    ?>
</html>

```

But I keep getting the following error message:

> 
> Warning: mysql\_connect() [function.mysql-connect]: Can't connect to MySQL server on 'localhost' (10061) in C:\xampp\htdocs\database\_connect.php on line 5
>  Could not connect: Can't connect to MySQL server on 'localhost' (10061)
> 
> 
> 

Here are the troubleshooting steps I took:

* Checked whether mysqld is running in Windows Task Manager Processes - it is
* Checked whether MySQL was running on the host by typing in Windows command prompt: "telnet 192.0.0.1 3306" and got the message "Could not open connection to the host, on port 3306: connection failed"
* Checked whether Windows Firewall was blocking MySQL - MySQL is an exception.

How do I get this code to work safely? And how do I check basic useful information about my MySQL like username?

Answer: 
Maybe you don't have remote connections set on the MySQL install. You're attempting to connect over a port, which is the same as a remote connection. It's been a while since I've done MySQL, but this is a dead giveaway:

> 
> Checked whether MySQL was running on
>  the host by typing in Windows command
>  prompt: "telnet 192.0.0.1 3306" and
>  got the message "Could not open
>  connection to the host, on port 3306:
>  connection failed"
> 
> 
> 

It's not going to work. Are you sure it's configured for port 3306 and not another port? Double check that.

The code itself looks fine and is not the issue. The port is clearly the issue.
I solved the same problem "Can't connect to MySQL server on 'localhost'" with the following steps (Windows 7, XAMPP v.3.2.1 installed, php connection to MySQL didn’t work):

1. Open XAMPP Control Panel (my version is v.3.2.1). Press button "Shell" on the right side of the panel.
2. Window opens with a title:
"Administrator: XAMP for Windows – mysql –u root" and with the following text:

> 
> 
> ```
> Setting environment for using XAMPP for Windows
> comp@COMP c:\xampp
> 
> ```
> 
> 

3. Input: `#mysql`

Obtained answer: `Welcome to the MySQL monitor…`

Then input: `mysql>create database mdb;` 

Answer: `ERROR 1044 (42000): Access denied for user ‘’@’localhost’ to database ‘drawdb’`

Input: `#mysql –u root` 

Answer: `Welcome to the MySQL monitor…`

Input: `mysql>create database mdb;` 

Answer: `Query OK, 1 row affected (0.00 sec)`

Input: `mysql>grant all on mdb.* to user@localhost identified by ‘password’;` 

Answer: `Query OK, 0 rows affected (0.06 sec)`

I.e. I allowed access to the manually created database to the specified user. After that php connection to MySQL with user “user” and password “password” could be successfully created:

```
> $servername = "localhost";  
> $username = "user";  
> $password = "password"; 
> // Create connection 
> $conn = new mysqli($servername, $username, $password);

```