I have a problem with my PDO class. I just have started learning OOP and I don’t really know where I made a mistake
My class code:
<?php
class admin
{
private $host = 'mysql:host=localhost;dbname=db501865';
private $username = 'root';
private $password = 'root';
private $conn;
public function connect() {
try {
$conn = new PDO($this->host, $this->username, $this->password);
} catch ( PDOException $e ) {
die( 'Connection failed: ' . $e->getMessage() );
}
return $conn;
}
public function disconnect( $conn ) {
$conn = '';
}
public function listReal()
{
$this ->connect();
$real = $conn->query('SELECT * FROM `real`');
echo '<ul>';
foreach ($real as $row)
{
echo'<li><img src="'.$row['image'].'"></li>';
}
$real -> closeCursor();
echo'</ul>';
}
}
?>
and after executing following code I have 500 error in my browser.
$db = new admin;
$db -> listReal();
Where I made a mistake?
You have to use
$this->var_namefor your member variables.Also there is no need to connect if you are already connected.