Ik heb een vreemd probleem met PHP PDO. Ik gebruik 2 connecties, 1 lokaal en 1 extern. Daarvoor gebruik ik 2 verschillende PDO instanties. Alleen kan ik niet naar buiten verbinden.
<?php
try{
$data = new PDO('mysql:host=....nl;dbname=shop', '...', '....');
$stmt = $data->prepare('SELECT id FROM customers LIMIT 10');
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
print_r($result);
}catch(PDOException $e){
echo '<pre>';
var_dump($e);
echo '</pre>';
}
?>
Dit geeft mij het volgende resultaat.
object(PDOException)#29 (8) {
["message":protected]=>
string(77) "SQLSTATE[HY000] [2003] Can't connect to MySQL server on '....' (110)"
["string":"Exception":private]=>
string(0) ""
["code":protected]=>
int(2003)
["file":protected]=>
string(35) "/var/www/includes/modules/login.php"
["line":protected]=>
int(4)
["trace":"Exception":private]=>
array(2) {
[0]=>
array(6) {
["file"]=>
string(35) "/var/www/includes/modules/login.php"
["line"]=>
int(4)
["function"]=>
string(11) "__construct"
["class"]=>
string(3) "PDO"
["type"]=>
string(2) "->"
["args"]=>
array(3) {
[0]=>
string(36) "mysql:host=...;dbname=..."
[1]=>
string(7) "..."
[2]=>
string(10) "..."
}
}
[1]=>
array(4) {
["file"]=>
string(18) "/var/www/index.php"
["line"]=>
int(52)
["args"]=>
array(1) {
[0]=>
string(35) "/var/www/includes/modules/login.php"
}
["function"]=>
string(12) "require_once"
}
}
["previous":"Exception":private]=>
NULL
["errorInfo"]=>
NULL
}
Als ik de normale MySQL functies los laat naar dezelfde host werkt het direct;
<?php
mysql_connect('...','...','...');
mysql_select_db('shop');
$query = 'SELECT id FROM customers LIMIT 10';
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)){
echo $row['id'].'<br />';
}
?>
Lokaal verbinden werkt overigens prima, het is dus enkel mijn server i.c.m. PDO en de externe MySQL server.
via commandline kan ik er ook gewoon bij.
jurgen@jurgen:~$ telnet [host] 3306
Trying 85.17.xxx.xxx...
Connected to [host].
Escape character is '^]'.
8
5.0.41-logXuH.P-,5e|`,M?6/(yNWKS=uConnection closed by foreign host.
jurgen@jurgen:~$ mysql -h [host] -u [username] -p
Enter password: ******
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4749147
Server version: 5.0.41-log Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
Hopelijk kunnen jullie mij helpen.