Call to a member function prepare() on a non-object with PDO 
<p>I have a function which uses:</p>

<pre><code>require_once("connect.php");

function get_username($uid){
  $stmt = $pdo-&gt;prepare("SELECT username FROM users WHERE uid= ?");

try {
    $stmt-&gt;execute(array($uid));
} catch (PDOException $e) {
    echo $e -&gt; getMessage(); exit;
}

      $row = $stmt-&gt;fetch();
      return($row['username']);
}

$id = 1;
echo get_username($id);
</code></pre>

<p>But it says :</p>

<pre><code>Call to a member function prepare() on a non-object
</code></pre>

<p>I'm a bit confused on what this means i have wrong, i'm wondering if its because I am using it in a function ?</p>

<p>EDIT:</p>

<p>My Connect script¬</p>

<pre><code>$dsn = 'mysql:dbname=test_db;host=127.0.0.1';
$user = 'test_user';
$password = 'test_pass';

try {
    $pdo = new PDO($dsn, $user, $password);

} catch (PDOException $e) {
    echo 'Connection failed: ' . $e-&gt;getMessage();
    exit;
}

$pdo-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

error_reporting(E_ALL);
session_start();
</code></pre>
