Forum Moderators: coopster

Message Too Old, No Replies

php & mySQL: localhost log in from non-localhost site

         

jason_m

3:43 pm on May 31, 2010 (gmt 0)

10+ Year Member



hello!

my question is the following.

i currently log in to mySQL db on my site using php as follows:
<? $host="localhost"; // Host name 
$username="myGuest"; // Mysql username
$password="myPW"; // Mysql password
$db_name="myDB"; // Database name
$tbl_name="myTBL"; // Table name
?>


it works great. i am able to do anything i need. but now, let's say i want to have a page on my local machine that i open to run some maintenance code on my DB's.

how do i go understanding what my "localhost" is? let's assume my site is www.mysite.com
is "www.mysite.com" my localhost?

any help would be largely appreciated!

Readie

3:48 pm on May 31, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't think you need to say "www."

I also think you need to specify a port number to use. Could do it with an IP aswell:

$host = 'example.com:3307';
$host = '192.168.0.1:3307';

Matthew1980

4:04 pm on May 31, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all,

What ever your IP address is on the machine you are running the server from is what you need to quote when you are 'logging in' from any other machine (provided there is a connection there of course ;))

And don't forget that the use of short tags (<?) is not used by most servers these days, if you want full portability use the full tags (<?php) this will save you headaches in the long run.

Cheers,
MRb

rocknbil

5:05 pm on May 31, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



how do i go understanding what my "localhost" is?


Localhost is a specific declaration of any mysql server running on the same machine as any web resource accessing it. That is, if you're running a test bed on your own computer, or the mysql server is running off your web server, the mysql server will always be "localhost."

mysql doesn't need the host declared if it's always localhost, it will default to it. But I think PHP may choke if you don't have the value. Try setting $host to '' to see if that's the case.

When you need to specify a host is when the mysql server is a physically or virtually different machine. so if your site is example.com, but your hosting service tells you your mysql server is mysql001234.example.com, the second is what you'd specify in $host. This is because, inherently, mySQL was designed to be accessed remotely. If there's no remote host, localhost is assumed.

topr8

5:11 pm on May 31, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



afaik, there are some webhosts who will block access to the mySQL server except from localhost (or from their own internal network) for security reasons.

jason_m

10:42 pm on May 31, 2010 (gmt 0)

10+ Year Member



thank you for the responses. truly appreciated.

let me specify, my localhost is my webpage hosted by my web host that i pay for.
therefore, my local machine is not also my localhost. my localhost is my site hosted externally on their machine that houses my site.'

re: Readie, would i ask my web host what my port is for my mySQL db? and from there go about doing my coding?

thanks!

Readie

11:33 am on Jun 1, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



AFAIK it's usually port 3306 - a quick Google of "port 3306" mentions a lot about mysql.

Depending on how the firewall has been set up though, may affect what port you need to use, so if using 3306 fails, you'd need to get in touch with your service provider.

optik

2:20 pm on Jun 1, 2010 (gmt 0)

10+ Year Member



You have to set up the privileges on the database correctly for it to be accessed from another computer, the port number shouldn't be necessary.

Generally this will mean adding a new user to the database that is designated access with a specific IP otherwise you can set the host to any host but this is obviously a security risk.