PHP Notice: Use of undefined constant localhost

I am getting the following error:

“PHP Notice: Use of undefined constant localhost - assumed ‘localhost’ in /var/www/file.php”

The line number I am getting is the first line of code below.

========= PHP CODE ================

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

	$query  = "SELECT URL FROM $type WHERE RAND='$rand' LIMIT 1";
	$result = mysql_query($query);
	
while($row = mysql_fetch_row($result))
{
return $row[0];	
}

What needs to be done. Thanks in advance for any help.

Thanks.

put quotes around localhost

Single or double quotes?

Preferably single, but the time you had taken to reply would have been sufficient to try both… :rolleyes:

My log is like of 80 Mb full of this errors, it takes more time to download, open and read it to check if the error still exists.

I really appreciate your help.

Thanks.

Just to explain what’s happened so you know next time.

You’ve used the constant ‘localhost’ without any quotes, because you hadn’t previously called defined('localhost) or define(‘localhost’, ‘localhost’); PHP looked in the constants table but couldn’t find localhost.

This in turn throws a warning, but also takes PHP to interpret that constant: localhost, as it’s actual string value. ‘localhost’.

This is another reason you should ensure you always use quotes when calling indexes in arrays (unless of course you want to call the constant value.)

E.g. $array[‘key’] is about 4-6x faster than $array[key] and also wouldn’t cause problems if you had define(‘key’, ‘blah’); before hand. If you had then you would actually look for the $array[‘blah’] key.

Hope this helps you understand,

Dan