Fatal error: Call to a member function prepare() on a non-object
Is what I am getting when I try to do:
$sql = $connect->prepare ("SELECT id, pwd, firstname, lastname, approved, user_level FROM users WHERE $user_cond AND banned = 0"); // SELECT
$sql->execute ();
$sql->setFetchMode(PDO::FETCH_ASSOC);
$num = $connect->rowCount();
Im holding on rewriting my system, taking out all mysql_* and make use of pdo instead.
This is what was before:
$result = mysql_query("SELECT `id`,`pwd`,`firstname`,`lastname`,`approved`,`user_level` FROM users WHERE
$user_cond
AND `banned` = '0'
") or die (mysql_error());
$num = mysql_num_rows($result);
What have I done wrong?
And with list() I have:
list($id,$pwd,$firstname,$lastname,$approved,$user_level) = mysql_fetch_row($result);
instead of mysql_fetch_row($result); what should I do in PDO?
My PDO object/connection:
try{
$connect = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME.";charset:UTF-8", DB_USER, DB_PASS, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
}
catch(PDOException $pe)
{
die('Could connect to the database because: ' .$pe->getMessage());
}
usually means that something failed and your variable is not what you expect it to be.
$connectionis probablyfalse.PDOStatement::fetch
Also call
rowCounton the statement, not on the connection: