Question

I would like to make a page for uploading some of my local records to the web server.

The Table Structure from local and web server are the same.

My question is, how to connect to both server?

<?php
$localCon = mysql_connect("localhost", "root", "");
if (!$localCon) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db("[table]", $localCon);

$serverCon = mysql_connect("mysql", "[username]", "[password]");
if (!$serverCon) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db("[table]", $serverCon);


$result = mysql_query("SELECT * FROM [table]");

while ($row = mysql_fetch_array($result)) {
    echo $row['[coloumn]'];
    echo "<br />";
}

mysql_close($localCon);
mysql_close($serverCon);
?>

I got error for both local and web server.

Or is there another way for me to upload the local records to the web?

This is the error msg:
Warning: mysql_connect() [function.mysql-connect]: php_network_getaddresses: getaddrinfo failed: No such host is known. in [addres]
Warning: mysql_connect() [function.mysql-connect]: [2002] php_network_getaddresses: getaddrinfo failed: No such host is known. (trying to connect via tcp://mysql:3306) in [addres]
Warning: mysql_connect() [function.mysql-connect]: php_network_getaddresses: getaddrinfo failed: No such host is known. in [address]
Could not connect:
Was it helpful?

Solution 2

The error is clear: Unless you have an entry for mysql in your hosts file, mysql is not a valid network address.

You need to specify the address of the remote server as the first parameter in your mysql_connect call.

Please note that the mysql_* functions are deprecated and it is recommended to switch to PDO or mysqli with prepared statements.

OTHER TIPS

You can connect to more databases but you need the handle in your query.

$result = mysql_query("SELECT * FROM [table]", $serverCon);

Otherwise you don't know from where you make the query and its not working.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top