I have the following situation:
$DB_Environment = file('../DB/DB_Environment.txt');
$host = trim($DB_Environment[0]);
$dbname = trim($DB_Environment[1]);
$p1 = "mysql:host=".$host.";dbname=".$dbname;
$user=trim($DB_Environment[2]);
$pass=trim($DB_Environment[3]);
try {
$conection = new PDO($p1, $user, $pass);
$conection->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
}catch (PDOException $e) {
echo $e->getMessage();
}
This is an fragment of code that gives me this error:
SQLSTATE[HY000] [2005] Unknown MySQL server host 'localhost' (2)
The code above finds the content on 1st to 4th lines in the .txt
file that contains the data to configure the connection with the database.
It apparently works on my localhost, but when I run it on the server, the error appears. Then I changed the code to this:
try {
$conection = new PDO("mysql:host=infojr.com.br;dbname=pluginfo", "myuser", "mypass");
$conection->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e) {
echo $e->getMessage();
}
I changed the search on the .txt
file by your content, and it works, but I want it to work like the first example, which only works on my localhost (PC).