使用PDO的未定义变量[重复]

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

You need to move the global $db; statement from where it is into the function scope.

See "Example #1 Using global" here.

You seem to be declaring $db as global before it has been declared.

A quick fix would be to pass your $db into the function register so it will be available inside this functions scope. Make sure you drop the global keyword from your $db variable first, and you will need to update any other scripts using the function register() and pass in the $db variable:

function register($username, $password, $email, $db){
//now you will have $db available inside this scope

Hope this helps, however as a sidenote this is rather clunky and you should probably focus on setting up your application to use classes instead.