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)
If you can access the MySQL shell, you can check the server's networking configuration like this:

```
mysql> show variables like 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port          | 3306  | 
+---------------+-------+

mysql> show variables like 'skip_networking';
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| skip_networking | OFF   | 
+-----------------+-------+
1 row in set (0.00 sec)

```

If `skip_networking` is set to 'ON', then your MySQL server has been configured to NOT allow access via the network and will function only via local sockets. Otherwise it'll be listening on the port specified in the `port` configuration variable.