简体   繁体   中英

How to connect mysql db in another host using PDO?

I don't know how connect into one mysql db in different host(server) to show medicament list in another website... can you help me with this?

here is the connection I use in the same server, but in different server I can't connect. Can you explain me how to do it?

<?php 
$dbtype     = "mysql";
$dbhost         = "localhost";
$dbname     = "XXXX_medicament";
$dbuser     = "XXXX_medicament";
$dbpass     = "123456789";
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
$conn->exec("set names utf8");
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
?>

here are the errors:

Fatal error : Uncaught exception 'PDOException' with message 'SQLSTATE[28000] [1045] Access denied for user 'XXXX_medicament'@'localhost' (using password: YES)' in /home/XXXX/public_html/peri/includes/config.med.php:7 Stack trace: #0 /home/XXXX/public_html/peri/includes/config.med.php(7): PDO->__construct('mysql:host=loca...', 'XXXX_medicament', '123456789') #1 /home/XXXX/public_html/peri/inicio.php(32): include('/home/XXXX/...') #2 /home/XXXX/public_html/peri/home.php(7): include('/home/XXXX/...') #3 {main} thrown in /home/XXXX/public_html/peri/includes/config.med.php on line 7

First, you have to check if the other database allows external connections. If it does, you have to change the database host address to the address of the mysql server.

Example:

$dbhost = "mysql.host.com";

or for an IP address

$dbhost = "123.123.123.123";

Simple then. You've got the password for user XXXX_medicament wrong! The username has a different password on the remote machine.

Try logging into the control panel provided by your web host and look for an option to configure databases. In there, you should be able to change passwords for users.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM