954,585 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Database function fail

function fafaconnect($db_host, $db_user, $db_pass, $db_name) {
	@mysql_connect($db_host, $db_user, $db_pass);
	@mysql_select_db($db_name);
}

if ($link = @mysql_connect("localhost", "admin", "pass")) {
	echo "pass-";
} else {
	echo "fail-";
}
if ($select = @mysql_select_db("zushee")) {
	echo "pass-";
} else {
	echo "fail-";
}

if ($newlink = fafaconnect("localhost", "admin", "pass", "zushee")) {
	echo "pass";
} else {
	echo "fail";
}

This returns pass-pass-fail. Any ideas?

grr
Light Poster
29 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

From the PHP Documentation:
Note: Whenever you specify "localhost" or "localhost:port" as server, the MySQL client library will override this and try to connect to a local socket (named pipe on Windows). If you want to use TCP/IP, use "127.0.0.1" instead of "localhost". If the MySQL client library tries to connect to the wrong local socket, you should set the correct path as Runtime Configuration in your PHP configuration and leave the server field blank.
Try changing localhost to 127.0.0.1 and see if this fixes your problem. If not, try echo'ing mysql_error() instead of "fail" as this will give you more details as to why your connect call is failing.

darkagn
Veteran Poster
1,197 posts since Aug 2007
Reputation Points: 404
Solved Threads: 200
 

I guess this have the second argument(connection) missing:

mysql_select_db($db_name)

try something like this:

$conn = mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db($db_name, $conn);
evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You