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: 
You can check that the MySQL server is bound to port 3306 using [tcpview](http://technet.microsoft.com/en-us/sysinternals/bb897437.aspx). More simply, drop the port from the host specifier. The driver should then attempt to use a named pipe, rather than a TCP socket.

On an unrelated note, I strongly urge you to switch to the [PDO](http://php.net/PDO) MySQL driver. The one you're using is terribly out of date. One big advantage is PDO supports [prepared statements](http://www.php.net/manual/en/pdo.prepared-statements.php), which offer security and efficiency benefits.

Edit:

This doesn't answer your main question, but posting this information in a comment would be a mess.

Rather than W3Schools, check out the resources suggested in:

* [What is the best PHP programming book?](https://stackoverflow.com/questions/90924/what-is-the-best-php-programming-book)
* [PHP tutorial that is security-, accuracy- and maintainability-conscious?](https://stackoverflow.com/questions/2119083/php-tutorial-that-is-security-accuracy-and-maintainability-conscious)
* [Good Resources for Relational Database Design](https://stackoverflow.com/questions/11615/good-resources-for-relational-database-design)
* [Relational database theory and SQL book recommendations?](https://stackoverflow.com/questions/983336/relational-database-theory-and-sql-book-recommendations)
* [How should a programmer learn great database design?](https://stackoverflow.com/questions/1028781/how-should-a-programmer-learn-great-database-design)
* [Database Design Best Practices](https://stackoverflow.com/questions/387339/database-design-best-practices)
* [Best Book for a new Database Developer](https://stackoverflow.com/questions/2018/best-book-for-a-new-database-developer)
**Note:** This is not the solution to the OP's problem, but I will keep my answer for historical reasons and also because it might be the issue for some other folks.

Are you on Windows Vista/7 and running PHP 5.3.1?

A recent change in the MySQL library in PHP right now (now using `mysqlnd`) is causing problems when connecting to `localhost`. A bug has been filled but until then, make sure that your `hosts` file contains an entry for `localhost`.

Your `hosts` file `localhost` entries must look like such:

```
127.0.0.1  localhost
#::1       localhost

```

As you can see, the `IPv6` entry is commented out and the `IPv4` entry isn't.

The `hosts` file is located at:

```
%WINDIR%\System32\drivers\etc\hosts

```

[Source](http://ca2.php.net/manual/en/function.mysql-connect.php#94316)