I am trying my hands on PDO for the first time. Issue is whenever I am running a PDO query I get a blank array in my browser
The code,
<?php
$config['db'] = array(
'host' => 'localhost',
'username' => 'root',
'password' => '',
'dbname' => 'website'
);
$db = new PDO('mysql:host=' .$config['db']['host']. ';dbname=' .$config['db']['dbname'], $config['db']['username'], $config['db']['password']);
//$query returns PDO statment object
$query = $db->query('SELECT `articles`.`title` FROM `articles`');
print_r($query);
//we will use different methods on PDO to work with database
//a generic method to display all results
while($rows = $query->fetch(PDO::FETCH_ASSOC)){
echo '<br>'.$rows['title'];
}
$rows1 = $query->fetch(PDO::FETCH_ASSOC);
print_r($rows1);
$rows2 = $query->fetchAll(PDO::FETCH_ASSOC);
echo '<pre>', print_r($rows2, true), '</pre>';
$rows3 = $query->fetchAll(PDO::FETCH_NUM);
echo '<pre>',print_r($rows3, true),'</pre>';
$articles = $query->fetchAll(PDO::FETCH_ASSOC);
echo $articles[4]['title'];
?>
The problem occurs while printing or echoing values for the variables $rows1, $rows2 & $rows3.
I should be getting pre formatted array but all I get is blank array, as shown
Let me know your inputs friends, thanks...