I'd like to select different data from my database using a function. Unfortunately I don't have access to $pdo
in the function. Here is the code for better understanding:
config.php
...
$pdo = new PDO("mysql:host=$host;dbname=$database", $user, $password);
select.php
include 'config.php';
function abc($sql) {
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':name', $name);
$stmt->execute();
}
$sql = "SELECT a FROM table WHERE b = :name";
abc($sql);
$sql = "SELECT a FROM table WHERE c = :name";
abc($sql);
Error:
Call to a member function prepare() on null
If I put the config.php into the function, it works like a charm.