Jump to content

trying to connect to mysql databse in another machine


suyesh.amatya

Recommended Posts

I have a php script in my machine and via it I want to access the database in the other machine with IP 192.168.0.104.

The code is:

 

        mysql_connect("192.168.0.104","root","");

mysql_select_db("dbtest");

$sql="select * from dbtest";

$result=mysql_query($sql);

while($data=mysql_fetch_array($result)){

echo $data['name'];

}

but i am not being able to do this.Need help..

Link to comment
Share on other sites

Use this code, and see if any errors appear:

 

mysql_connect("192.168.0.104","root","") or die(mysql_error());
mysql_select_db("dbtest") or die(mysql_error());
$sql="select * from dbtest";
$result=mysql_query($sql) or die(mysql_error());
while($data=mysql_fetch_array($result)){
    echo $data['name'];
}

Link to comment
Share on other sites

first off, PUT A PASSWORD ON YOUR ROOT ACCOUNT

 

Secondly,

log into the remote server and add a user with remote connection privileges:

 

CREATE USER 'test'@ '192.168.3.198' ......

 

where the IP or hostname is the ip or hostname of the computer that will be connecting to the server.  If you wish to allow any computer to connect, use %.  Finish the CREATE statement with the required privileges and database restrictions.

 

DO NOT:

 

1. grant root remote connections (especially with no password)

2. grant the remote user more privileges then he needs (if he doesn't need to be able to alter or drop tables, don't let him)

3. give the remote user admin rights (create users, grant permissions, lock tables, etc...)

4. give the remote user a null or weak password 

 

This query string should do what you want and be reasonably secure:

 

CREATE USER '<user>'@'<host>' IDENTIFIED BY '<passwd>';
GRANT SELECT , INSERT , UPDATE , DELETE ON `<database>` . * TO '<user>'@'<host>';

change the stuff identified by < > tags

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.