I've been searching and testing different approaches to fix my issue but I still can't find the damn solution. I'm new to PHP and i'm trying to teach myself working with PDO.
db.php:
<?php
$dsn = 'mysql:myHost=localhost;dbname=ewt';
$user= 'root';
$pass = '';
try {
$pdo = new PDO($dsn, $user, $pass);
$pdo ->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
showCust.php
<?php
include_once('db.php');
try {
$sql = "SELECT * FROM PERSOON";
$result = $pdo->query($sql);
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
echo $row['VOORNAAM']. ' - '. $row['NAAM']. ' - '. $row['EMAIL']. ' - '. $row['ADRES']. '<br />';
}
$pdo = null;
}
catch(PDOException $e) {
echo $e->getMessage();
}
It works perfecty when I copy the db.php code in the showCust.php but I want to split the DB connection from every other file. I know it has to do something with the scope of $pdo but I just can't figure it out ...
The error i keep getting are:
Notice: Undefined variable: pdo ... line 9
Fatal error: Call to a member function query() on a non-object in ... line 9
I know the database does NOT have a pw. This is purely for test purposes.
Thanks in advance !