call to a member function query() on a non-object error
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/3726505/how-to-squeeze-error-message-out-of-pdo">How to squeeze error message out of PDO?</a>  </p>
</blockquote>



<p>Hi I am new to php and trying to create a conenction to a database and I am getting a Call to a member function query() on a non object error:</p>

<p>My code is separated into two files the first file contains the function for creating a connction:</p>

<pre><code>    &lt;?php 
function dbConnect($usertype, $connectionType = 'mysqli'){
    $host = 'localhost';
    $db = 'phpsols';
    if($usertype == 'read'){
        $user = 'psread';
        $pwd = 'Alexandru&amp;Mada';
    }elseif ($usertype == 'write'){
        $user = 'pswrite';
        $pwd = 'Alexandru&amp;Mada';
    }else{
        exit('Unrecognized connection type');
    }
    if ($connectionType == 'mysqli'){
        return new mysqli($host, $user, $pwd, $db) or die('Cannot open database');
    }else {
        try{
            return new PDO("mysql:host=$host; dbname=$db", $user, $pwd);
    }  catch (PDOException $e){
        echo 'Cannot connect to database';
        exit;
    }
    }
}
?&gt;
</code></pre>

<p>In the second file this method is called but it also throws an error call to a member function query() on a non object:</p>

<pre><code>&lt;?php
require_once ('../includes/connection.inc.php');
$conn = dbConnect('read');
$sql = 'SELECT * FROM images';
$result = $conn-&gt;query($sql) or die(mysqli_error()); //error is thrown here
$numRows = $result-&gt;num_rows;
?&gt;
</code></pre>

<p>What am I doing wrong here?</p>
