Thread: Define localhost In a Program

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    193

    Define localhost In a Program

    Was wondering if I wanted to connect to a database and in PHP I would normally use localhost, what would I use for C++? I don't have MySQL++, I have some MySQL++ look-a-like and it didn't come with a tutorial.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    localhost is defined via the OS, not by your program. You can use localhost when it asks for an ip address or host name. You could also use 127.0.0.1

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    I'm trying to connect to my database on the internet for my site using a program and it says it can't connect to the database. I'm positive I have the database correct, but not sure about localhost because I thought localhost meant the address to the mysql databases. I guess I'm confused.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    No, localhost is defined by your OS. For instance, you can open up a command window and type in "ping localhost". localhost is just defined as your own machine.

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    210
    Quote Originally Posted by bithub
    localhost is just defined as your own machine.
    Right, this might be confusing because in the case of PHP, which is executed on the machine that runs the webserver, localhost refers to that machine and not ... say, the machine running the webbrowser through which you look at your php site.
    PHP is executed on the webserver, so localhost in a PHP program refers to that server.

  6. #6
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    Quote Originally Posted by Nyda
    Right, this might be confusing because in the case of PHP, which is executed on the machine that runs the webserver, localhost refers to that machine and not ... say, the machine running the webbrowser through which you look at your php site.
    PHP is executed on the webserver, so localhost in a PHP program refers to that server.
    So you're saying that localhost is defined by the server and not your machine when using PHP? And if so, how would I get to the server with C++?

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    So you're saying that localhost is defined by the server and not your machine when using PHP?
    No, Nyda is saying that localhost refers to the machine that the code is being run on. With PHP, the code is run on the server, so localhost refers to the server.

    And if so, how would I get to the server with C++?
    This depends. Is the C++ code going to be run on the server, or the client machine?

    If your c++ program is going to be run on the server, then localhost will refer to the server.
    If your program is going to be run on the client, then you need the domain name or ip address of the server to access it.

  8. #8
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    Quote Originally Posted by bithub
    No, Nyda is saying that localhost refers to the machine that the code is being run on. With PHP, the code is run on the server, so localhost refers to the server.


    This depends. Is the C++ code going to be run on the server, or the client machine?

    If your c++ program is going to be run on the server, then localhost will refer to the server.
    If your program is going to be run on the client, then you need the domain name or ip address of the server to access it.
    That makes sense now. Thanks for the info and both of your help is much appreciated, but I have one last question. I'm using the IP Address instead of localhost and now I can't select a database. I'm using the same settings as I would for PHP. Do I need to change the database, or username, or password in my program? Do I need to allow outside access on the server (And if so, how would I do that)? Sorry if I'm asking for too much.

  9. #9
    Registered User
    Join Date
    Apr 2004
    Posts
    210
    Quote Originally Posted by stickman
    Do I need to allow outside access on the server.
    Probably, because you are not connecting that DB server from it's own hostmachine.

    Quote Originally Posted by stickman
    (And if so, how would I do that)?
    I don't know. This is better be asked in a MySQL board or newsgroup. It might also well be your webhost won't permit outside access.

  10. #10
    umm i didnt read the rest of the post, so ill just
    throw my two cent in real quick.

    localhost = 127.0.0.1 = the computer that your on
    no matter where your at if its a windows computer, to access
    itself you can use localhost or 127.0.01 , this is internal
    ip address you could think of it like that. its a loopback
    address, hope that clears it up more.

  11. #11
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    localhost = 127.0.0.1 = the computer that your on
    no matter where your at if its a windows computer
    well technically localhost is whatever the HOSTS file says it is. This file is in c:\windows\system32\drivers\etc on XP machines (not sure where it is on other windows versions).

  12. #12
    lol need to be specific do we, that wasnt anyways
    it was just my point cause if i remember it doesnt
    apply whatsoever to unix/linux systems

  13. #13
    Registered User
    Join Date
    Apr 2004
    Posts
    210
    Quote Originally Posted by JarJarBinks
    if i remember it doesnt
    apply whatsoever to unix/linux systems
    It does. The file is usually called /etc/hosts and could look like ...

    Code:
    nyda, nyda/ $ cat /etc/hosts
      127.0.0.1 traveltux localhost localhost.localdomain
      
      # IPV6 versions of localhost and co
      fe00::0 ip6-localnet
      ff00::0 ip6-mcastprefix
      ff02::1 ip6-allnodes
      ff02::2 ip6-allrouters
      ff02::3 ip6-allhosts
      
      
      
      # follows:AD removal
      [...]
    Last edited by Nyda; 09-13-2004 at 11:14 AM.

  14. #14
    Registered User
    Join Date
    Feb 2004
    Posts
    35
    Quote Originally Posted by bithub
    well technically localhost is whatever the HOSTS file says it is. This file is in c:\windows\system32\drivers\etc on XP machines (not sure where it is on other windows versions).
    Not present by default anywhere in 98SE other than the sample files (.SAM), you may however create one directly in the windows folder.

  15. #15
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    How do I add access from my IP Address to the server so I can connect to the databases? I totally understand the loaclhost thing, but I tried adding my IP Address as an access host and that didn't work. Shall I ask this on a mysql/cpanel forum? Anyway, thanks for all of your help. It's been much appreciated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Would someone solve my problem?
    By Lonners in forum C Programming
    Replies: 9
    Last Post: 01-19-2008, 06:58 PM
  3. Adding buttons, edit boxes, etc to the window
    By rainmanddw in forum Windows Programming
    Replies: 1
    Last Post: 04-10-2006, 03:07 PM
  4. FAQ: Directional Keys - Useing in Console
    By RoD in forum FAQ Board
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM