i've been coding php for quite a while now but i recently got into object oriented programming.

Before i used mysql\_connect(),mysql\_query() and all those functions but then i heard that PDO would be way better alternative.

I thought why not and watched a basic PDO video.

```
try {
    global $db;
    $db = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pw);
} catch (PDOException $e) {
    die("ERROR: " . $e->getMessage());
}

print(gettype($db)."<br>");

function test() {
print(gettype($db));
}

test();

```

So the problem here is, i want to create a function so i can easily insert new items into my table. But i ran to this problem, my $db is not global. So i would have to re-connect to my database when using the function, but i can't do that, it would not be good at all.

So how can work around this problem?
Oh and the above code outputs:

```
object
Notice: Undefined variable: db in /home/vhosts/ollie.ceify.net/www/ip-bless/connect.php on line 25 NULL

```