I am a beginner in PDO. I am following a tutorial to learning PDO. I want to use a simple select statement to fetch id of users.
but when I run index.php it dont show any echo ! where is my wrong ?
I have four files :
config => setting username and password...
DB_Connect :
class DB_Connect {
// constructor
function __construct() {
}
// destructor
function __destruct() {
// $this->close();
}
// Connecting to database
public function connect() {
require_once 'include/config.php';
try {
$hostname = DB_HOST ;
$dbname = DB_DATABASE;
$dbh = new PDO("mysql:host=$hostname;dbname=$dbname", DB_USER, DB_PASSWORD);
}
catch(PDOException $e)
{
echo $e->getMessage();
}
return $dbh;
}
}
DB_Functions :
class DB_Functions {
private $db;
//put your code here
// constructor
function __construct() {
require_once 'DB_Connect.php';
// connecting to database
$this->db = new DB_Connect();
$this->db->connect();
}
// destructor
function __destruct() {
}
function getUsers(){
$sql = "SELECT * FROM users";
foreach ($this->$db->query($sql) as $row)
{
echo $row->id;
}
/*** close the database connection ***/
// $db = null;
}
}
index.php
<?php
require_once 'include/DB_Functions.php';
$qr = new DB_Functions();
$qr->getUsers();
?>
$this->$db->query($sql)to$this->db->query($sql)var_Dump($row)error_reporting(E_ALL); ini_set('display_errors', '1');on the top of the page and see what error u get