pdo fetchAll function returns an empty array

Mark wallest

i know it might be a duplicate question but i really didn't find any help reading other replies that's why i decided to post my issue i'm trying to pull out some data using pdo with prepared statement but i always get an empty array even though my query seems to be ok here is my code any help would be appreciated .

$host = 'localhost';
$dbname = 'dbname';
$password = 'xxxxx';
$user = 'xxxxx';
try{

$dbh = new PDO("mysql:host=$host;dbname:$dbname",$user,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_WARNING);
$sth = $dbh->prepare("SELECT * FROM subregions");
$sth->execute();
$result = $sth->fetchAll(PDO::FETCH_ASSOC);
var_dump($result);
/*foreach($result as $row){
    echo "<li>$row['name']</li>";
}*/
}catch (PDOexception $e){
  $e->getMessage();
  die();
}
?>
Amit Shah

try this, it should work.

<?php
/* Connect to an ODBC database using driver invocation */
$dsn = 'mysql:host=127.0.0.1;dbname=test;';
$user = 'root';
$password = '';

try {
    $dbh = new PDO($dsn, $user, $password);
    $sth = $dbh->prepare("SELECT * FROM email_table");
    $sth->execute();
    echo $sth->rowcount();
    $result = $sth->fetchAll(PDO::FETCH_ASSOC);
    var_dump($result);
} catch (PDOException $e) {
  $e->getMessage();
}
?>

Thanks Amit

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related