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?
You can check that the MySQL server is bound to port 3306 using tcpview. 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 MySQL driver. The one you’re using is terribly out of date. One big advantage is PDO supports prepared statements, 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: