It's possible to create a mysql connection from localhost to another server using php, if it's possible, how? Thanks!
Asked
Active
Viewed 778 times
0
-
Change the connection settings, user/password/server. EX: $dbh = new PDO('mysql:host=the_server_address;dbname=test', $user, $pass); – ka_lin Jan 22 '15 at 13:01
-
possible duplicate of [Connecting to remote MySQL server using PHP](http://stackoverflow.com/questions/1935314/connecting-to-remote-mysql-server-using-php) – Plamen Nikolov Jan 22 '15 at 13:02
-
One important thing is that your remote server should allow remote connections. http://stackoverflow.com/a/14779244/1501051 – عثمان غني Jan 22 '15 at 13:15
-
it is simple, i just want to create a connection from my localhost pc to my domaint http://www.something.something, to read the mysql db from domain, and administation from localhost, to don't connect to my server and disconect, and to this forever – Eugen Paici Jan 22 '15 at 14:09
4 Answers
1
$host_name = "www.yourdomain.com";
$database = "pdo"; // Change your database name
$username = "root"; // Your database user id
$password = "test"; // Your password
try {
$dbo = new PDO('mysql:host='.$host_name.';dbname='.$database, $username, $password);
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}

Schtro
- 11
- 2
0
Yes it is possible.You must have the username and password of that domain in which you want to connect.
mysqli_connect("www.domain.com","username","password","database_name")or die("Error " . mysqli_error());

Rahul
- 5,594
- 7
- 38
- 92
-
-
@UnicoRahul username password is needed for the database not for domain (host) – عثمان غني Jan 22 '15 at 13:21
-
@عثمان غني The question is about mysql connection and mysql connection means he wants to connect with database – Rahul Jan 22 '15 at 13:23
-
"You must have the username and password of that domain..." so I am saying – عثمان غني Jan 22 '15 at 13:23
-
0
Yes, Simply pass following details about your server:
<?php
$servername = "your-server-name-or-ip";
$username = "your-server-username";
$password = "your-server-password";
// Create connection
$conn = mysqli_connect($servername, $username, $password);

Ruprit
- 733
- 1
- 6
- 23
0
$url = 'mysql:host=xxx.xxx.xxx.xxx;dbname=xxxxxx'
$username = xxx;
$password = xxx;
$db = new PDO($url, $username, $password);
$query = $db->query('select * from some_table');
$query->execute();
$res = $query->fetchAll(PDO::FETCH_ASSOC);

zafirov
- 392
- 1
- 3
- 18