SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2007
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    put quotes around localhost
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2007
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Single or double quotes?

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Preferably single, but the time you had taken to reply would have been sufficient to try both...

  5. #5
    SitePoint Enthusiast
    Join Date
    Apr 2007
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  6. #6
    SitePoint Member
    Join Date
    Mar 2008
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •