我目前正在将这段代码从mysql转换为PDO:
$catquery = mysql_query("SELECT category, longdescription, locked,
catid, parentcatid, leveldown
FROM ".$tableprefix."fanfiction_categories
WHERE catid = '$catid'")
or die(_FATALERROR."Query: SELECT category, locked, catid, parentcatid, leveldown FROM ".$tableprefix."fanfiction_categories WHERE catid = '$catid'<br />Error: (".mysql_errno( ).") ".mysql_error( ));
$category = mysql_fetch_array($catquery);
我是PDO的新手,但一直在努力学习我的能力。这就是我在PDO中提出的:
$db = new PDO('mysql:host='.$db_host.'; dbname='.$db_name, $db_user, $db_pass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$catquery = $db->prepare("SELECT category, longdescription, locked, catid,
parentcatid, leveldown
FROM ".$tableprefix."fanfiction_categories
WHERE catid=:catid");
$stmt -> bindPARAM (':catid', $catid, PDO::PARAM_STR );
try {
$stmt->execute ();
echo "Sucess";
} catch (PDOException $e) {
echo $e->getMessage();
}
即使在以多种形式尝试之后,我也无法正确获取bindPARAM部分。有什么我可以忽略的吗?我对PDO有点困惑,所以如果有人能指出我在哪里出错,那将会很好。
答案 0 :(得分:1)
很简单,您忘记修改复制/粘贴中的所有变量
$db = new PDO('mysql:host='.$db_host.'; dbname='.$db_name, $db_user, $db_pass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$catquery = $db->prepare("SELECT category, longdescription, locked, catid,
parentcatid, leveldown
FROM ".$tableprefix."fanfiction_categories
WHERE catid=:catid");
//$stmt -> bindPARAM (':catid', $catid, PDO::PARAM_STR );
$catquery -> bindPARAM (':catid', $catid, PDO::PARAM_STR );
try {
//$stmt->execute ();
$catquery->execute ();
echo "Sucess";
} catch (PDOException $e) {
echo $e->getMessage();
}