0

It's possible to create a mysql connection from localhost to another server using php, if it's possible, how? Thanks!

4
  • Change the connection settings, user/password/server. EX: $dbh = new PDO('mysql:host=the_server_address;dbname=test', $user, $pass); Commented Jan 22, 2015 at 13:01
  • possible duplicate of Connecting to remote MySQL server using PHP Commented Jan 22, 2015 at 13:02
  • One important thing is that your remote server should allow remote connections. stackoverflow.com/a/14779244/1501051 Commented Jan 22, 2015 at 13:15
  • it is simple, i just want to create a connection from my localhost pc to my domaint 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 Commented Jan 22, 2015 at 14:09

4 Answers 4

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();
}
Sign up to request clarification or add additional context in comments.

Comments

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());

5 Comments

please mark it useful if it works for you
@UnicoRahul username password is needed for the database not for domain (host)
@عثمان غني The question is about mysql connection and mysql connection means he wants to connect with database
"You must have the username and password of that domain..." so I am saying
عثمان غني that is obvious
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);

Comments

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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.