I am a newbie on PHP . Can someone tell me how to connect and retrieve data from mysql database server using PHP from a different computer.
First of all, the MySQL server must accept remote connections.
Also MySQL's user that you use, must be allowed to log from PHP computer's IP.
Then you can simply connect from your PHP server changing configuration of connection by setting host parameter to MySQL server's IP instead of localhost.
Google for mysql remote connection in order to find information about how to configure it to accept remote connections.
Localhost in name suggest locally , you cannot access mysql database from another computer .
Copy the file and install , only then it will work.
Related
I have been recently using "localhost" as my mysql database, and now i want to update my website to web.
I took free hosting at "serversfree.com" and uploaded all my web files there.Now i have problem.I made mysql database on that website, and uploaded my mysql database from localhost.
I can go to that server and list all files i have in that mysql database, but when i want to connect my .php documents i don't know how.What to put under "hostname" (i was recently using only localhost).Do i need to put a link where my web mysql database is?
Or i need to put a link where my website is?
I tried both but didn't worked for me
Most web hosts I've used have MySQL running on the same box so I usually can keep localhost as the server.
Your host should have some documentation on where to point the server to.
Check if your database credentials, are from a valid user in that database..
What hostname did you use to upload your mysql database from localhost? It would be the same hostname. Probably mysql.serversfree.com. If their mysql server is on the same subdomain then you can use localhost as well. To connect your php scripts to a mysql server you need to use the hostname of the mysql server, not the hostname of your http server.
If your mysql is running in the same server then you can use localhost or 127.0.0.1 but if mysql is in another server probably you need remote connections
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
connecting external mysql database with php
I have desingned a form. The Data entered in the form, gets saved on my local machine database as well as on server database when i execute it on my localmachine using wamp. The same file i uploaded it on server, when i enter the data, the data gets saved only on my server database. Data is not getting saved on my local database. Is there any method to connect to local machine database from server.
Language i used for coding is php/mysql.
Is there any method to connect to local machine database from server
Yes there is. You just configure the local database to accept connections from the remote host your script is running on and give your script the WAN hostname and port of your database server.
This is nothing that special, sort of standard database configuration. The database vendor will offer you more dedicated support in your individual issue for your specific configuration, you might want to try as well the Mysql support forums and mailinglist.
Of course you can ... only need is: you need a dns or ip for the server-connection (you can simple use dyndns to get one for free) and you need to open the port 3306 on your "modem" or what ever and forward-it to your wamp-server ....
I'm trying to connect to a remote server's mysql instance from PHP on my main server.
The main server itself has no mysql installed but the server I'm trying to connect to has mysql installed.
PHP is installed on both servers but I was wondering if it would connect when mysql isn't installed locally.
You only need a client mysql library specific to your database connection stack, not a mysql server installation.
There's no reason that I can see for it not working. Keep in mind that you'll need the hostname of the server and the server will have to have remote_access configured.
All you need is to have the second server's URL and SQL info such as username and password.
no reason for it not to work.
I have been trying to connect to mysql all evening using PHP.
I know for a fact the username and password are correct as I have tried 3/4 and I know the permissions are set correctly in the database.
If I remote onto this machine and enter the exact same details into the commandline then i can connect to the database without issue.
The problem only seems to be with MYSQL and PHP.
A little more info:
I am trying to run a PHP app on the same server as the mysql database is on. The user has full grant permissions on the local machine. The error i am receiving is :
Fatal error: Attempt to connect to database kmc_cms on
The server is set so no remote access to mysql is available.
In your question you don't show whether you're trying IP or localhost.
Do a
show grants for <username>;
and see if the connection you're trying is even allowed.
Have you enabled remote access for the user you're trying to connect with in mysql?
I found out the issue. Within the mysql database config file there was an error which resulted in the connection allowing users from local host access only. Even though the PHP scripts were connecting using localhost it just would not authenticate. Hence I could log in using command line and not using PHP or other applications.
How can I connect my localhost PHP files to my remote server MYSQL database?
For example: if we download WordPress, and it gives us an option to login, so that our login credentials are saved with WordPress but still our localhost files connects to that database.
How do I do that?
Thanks.
If you want your PHP code to connect to a remote MySQL database, you just have to specify that remote host when calling the function that connects to MySQL.
For example, when calling mysql_connect(), don't specify localhost as first parameter -- but your real remote host.
Same with mysqli::__construct() or PDO, of course.
A couple of notes :
Your remote host must accept connections to MySQL (think about the firewall)
Your remote MySQL database server must accept connections from non-localhost
Sending queries, and fetching results, to/from a far away server will be a lot slower than doing so on localhost !
mysql_connect(SQL_IP, SQL_NAME, SQL_PASS);
Well when you install Wordpress set the host to be the IP of your remote server. If this isn't what you're looking for, please reword your question. If you're not using word press, check ou the PHP documentation for mysql_connect. The first parameter is the host. Enter the IP or hostname of your remote server here. And then follow that with your username and password.
mysql_connect('remote-host', 'myuser', 'mypassword');
Note: Some hosts do not allow remote connections to MySQL. Check your remote server doesn't have 3306 firewalled or only instructed MySQL to bind it to 127.0.0.1.