使用 php 从本地主机连接到另一个服务器的数据库


Connection to a db from localhost to another server with php

可以使用

php创建从localhost到另一台服务器的mysql连接,如果可能的话,如何?谢谢!

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

是的,这是可能的。您必须具有要连接的域的用户名和密码。

mysqli_connect("www.domain.com","username","password","database_name")or die("Error " . mysqli_error());
是的

,只需传递有关服务器的以下详细信息:

<?php
$servername = "your-server-name-or-ip";
$username = "your-server-username";
$password = "your-server-password";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
$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);
相关文章: