trying to get an array of databases in localhost using PDO
$user = 'root';
$server = 'localhost';
$db = new PDO("mysql:host=$server", $user);
$sql = "show databases";
$st = $db->prepare($sql);
$st->execute();
$arr = $st->fetchAll(PDO::FETCH_ASSOC);
foreach($arr as $el){
echo $el . '<br>'; // error - array to string conversion
}
also tried:
$arr = $st->fetch(PDO::FETCH_ASSOC);
result - only the first database is echoed
Any help?
var_dump($el);to see what was returned from the statement - since you're asking forFETCH_ASSOC, an associative array will be returned with a key for each value you're looking for.echo $el . '<br>';=>print_r($el) . '<br>';