douzhuan4406 2016-10-01 14:41
浏览 37

无法弄清楚我在准备好的声明中做错了什么[重复]

This question already has an answer here:

I'm currently converting this piece of code from mysql to 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);

I'm new to PDO but have been trying to learn what I can. This is what I came up with in 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();
}

I can't get the bindPARAM portion correct, even after trying it in many forms. Is there something I'm overlooking? I'm a bit confused in regards to PDO, so it would be nice if someone could point out where I'm going wrong with this.

</div>
  • 写回答

1条回答 默认 最新

  • duanhongqiong9460 2016-10-01 15:07
    关注

    Quite simply you have forgotten to amend all the variables from your copy/paste

    $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();
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来