I've been scratching my head over this for a few days now and I've still got nowhere.
I'm trying to pull a small set of values from a MySQL database via PHP PDO; I know PDO works as I am using it else where and I have based my code around te previously working code.
function custom_sub_cat_list($db_details, $cat_id) { //ln21
$subcat = NULL;
try {
$h = new PDO("mysql:host=".$db_details['host'].";dbase=".$db_details['db'],$db_details['user'],$db_details['pass']);
$h->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
} catch(PDOException $ex) {
return false;
}
try {
$q = $h->prepare("SELECT category FROM :tbl WHERE parentid = :catid;");
$q->bindValue(":tbl", $db_details['tbl'], PDO::PARAM_STR);
$q->bindValue(':catid', $cat_id, PDO::PARAM_STR);
$q->execute();
while($row = $_query->fetch()) {
$subcat['id'][] = $row['categoryid'];
$subcat['name'][] = $row['category'];
};
return $subcat;
} catch(PDOException $ex) {
return false;
}
}//ln49
I am getting "Call to a member function bindValue() on a non-object" on the bindValue's and being called up like below.
$cat_id = 123;
$db_details = array(
"host" => $sql_host,
"db" => $sql_db,
"user" => $sql_user,
"pass" => $sql_password,
"tbl" => $sql_tbl['categories']
);
custom_sub_cat_list ($db_details, $cat_id)
I'm sure it's something glaringly obvious but I can't see the problem and would like a fresh pair of eyes.
WORKING VERSION BELOW
Thank You Very Very Much! to everyone who helped, I've learnt a few bits too :-) There was some silly mistakes in there that I had overlooked, I just blame looking at it for two days solid.
function custom_sub_cat_list($db_details, $cat_id) {
$subcat = NULL;
try {
$h = new PDO("mysql:host=".$db_details['host'].";dbname=".$db_details['db'].";charset=utf8",$db_details['user'],$db_details['pass']);
$h->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$h->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$q = $h->prepare("SELECT category, categoryid FROM ".$db_details['table']." WHERE parentid = :cid;");
$q->bindParam(':cid', $cat_id, PDO::PARAM_INT);
$q->execute();
while($row = $q->fetch()) {
$subcat['id'][] = $row['categoryid'];
$subcat['name'][] = $row['category'];
};
$h = NULL;
return $subcat;
} catch(PDOException $ex) {
print_r($ex->getMessage());
print_r($ex->getTrace());
//return false;
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…