Hey everyone!
I've created my db.php and all my other scripts are fine.. just when I created a new page and included db.php I created a function. In the function I used my $dbh variable and it is throwing an error...
Notice: Undefined variable: dbh in C:\xampp\htdocs\Crafting Bench Server Website\inc\getinfo.php on line 6
Fatal error: Call to a member function prepare() on a non-object in C:\xampp\htdocs\Crafting Bench Server Website\inc\getinfo.php on line 6
Here is my getinfo.php:
<?php
try {
require('db.php');
function getInfo($user_row, $mcusername) {
$SQL = $dbh->prepare('SELECT * FROM users WHERE mcusername=:mcusername');
$SQL->bindValue(':mcusername', $mcusername, PDO::PARAM_STR);
$SQL->execute();
$user_row = $SQL->fetchColumn();
return $user_row;
}
} catch (PDOException $e) {
$e->getMessage();
}
?>
and well my db.php already works so if you need that let me know :D
I hope somebody can help me! I tried setting the $dbh to be global, but that didn't remove the fatal error warning and is generally just very bad practice in php, cheers!