Cant get php to connect to my mysql database locally

Since I am new to PHP, I am now trying to connect to mysql database and following some examples.

My php is as follows

<?php

// Connect to the database server
$dbcnx = @mysql_connect(‘localhost’,‘root’,‘pwd’);
if (!$dbcnx) { echo( “<P>Unable to connect to the database server at this time.</P>” );

             exit();  }

When I run it, it seems like its trying to connect before it returns the message “Unable to connect to the database server at this time.”

I installed mysql and am using the mysql workbence to play with queries… so I know it exists. I even tried localhost:3306 and also put the servername… but something doesnt seem to work correctly…

ARe there any PHP.ini settings that need to change or do I need to install something or change some IIS settings ? I am using PHP 5.3.3 , mysql 5.1.46 and IIS 7… All on Windows

You could try editing:
C:\Windows\System32\drivers\etc\hosts

But your DNS system really should do that by default. You could also try pinging localhost.

And unless you plan on deploying on a Windows Server then you might consider switching to Apache using XAMPP or something similar.

It worked when I changed localhost to 127.0.0.1. I saw that while googling around for a solution… But dont know why it doesnt like localhost… Even putting the name of the server, it didnt like… It just worked with that IP address… strange

Using Windows 7…

here is a function i use for all my websites.




function con_mysql()
{
	$connection=mysql_connect("localhost", "root", "pwd");
	mysql_select_db("dbname", $connection);
	return ($connection);
}

//activate the function
$db=con_mysql();