Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”
I am new to PDO, and i am currently trying to convert all of my mysql_query
's to PDO->
and I keep getting an error of an undefined variable $db
here is my code for the Database connection page:
$host = "localhost";
$user = "root";
$password = "";
$dbname = "XXXX";
global $db;
$db = new PDO("mysql:host=$host;dbname=$dbname;charset=UTF8", $user, $password);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
Edit: my register function that is having problems
<?php
###########################
# #
# Database Authentication #
# #
###########################
require('Database.php');
require('Bcrypt.php');
require('Session.php');
function register($username, $password, $email){
if($username != null && $email != null && $password != null){
/*
$check = $database->prepare("SELECT * FROM users WHERE Username = '$username' OR Email = '$email'");
$sql = $check->execute();
if(count($check) > 0){
echo "The username or email address you entered is already in use, please try another combination";
}
I currently have this commented out to test the INSERT query below
*/
if(true){
$salt = create_salt($username);
$password = hash_pass($password, $salt);
$query = $db->prepare("INSERT INTO users (Username, Password, Email) VALUES('$username', '$password', '$email')");
$query->execute();
return true;
}
else{
echo "Something went wrong with inserting into table";
return false;
}
}
else{
echo "Please fill in all of the information in order to register";
return false;
}
}
When running this i get
Notice: Undefined variable: db in ...\Authentication.php on line 33