dsh125986083 2017-02-19 21:38
浏览 37
已采纳

PHP查询不能使用bindParam,但查询本身可以正常工作

I'm trying to insert information insto a mySQL database and I get the WSOD and this error:

PHP Fatal error: Call to a member function bindParam() on a non-object ...

This is the code:

try {
        $conectar1 = new PDO('mysql:host='.HOST.'; dbname='.DATABASE.'; charset=utf8', USER, PASS); 

    $guardarPost = $conectar1->query("
        INSERT INTO foro 
        (userID, estadoPost, asuntoPost, postUltimo, datosPost)
        VALUES (?, ?, ?, ?, ?)
    ");
    $guardarPost->bindParam(1, $userID); <============ ERROR LINE
    $guardarPost->bindParam(2, $estadoMensaje);
    $guardarPost->bindParam(3, $asuntoPost);
    $guardarPost->bindParam(4, $fechaMensaje);
    $guardarPost->bindParam(5, $datosPost);
    $ok = $guardarPost->execute();
} catch (PDOException $e) {
    echo "Error ".$e->getMessage();
}

I've tried stripping everything to check is the DB connection work, and it does.

I've tried entering the query manually into phpMyAdmin replacing the question marks for the text, and it does work.

Where's the problem?

  • 写回答

1条回答 默认 最新

  • doutun1875 2017-02-19 21:44
    关注

    Try print_r($conectar1->errorInfo());

    also use prepare() instead of query() to use bindParam()s

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

    报告相同问题?

    悬赏问题

    • ¥15 Python词频统计,运行出的Excel没有内容
    • ¥15 求推荐一个好用的录屏软件
    • ¥15 kali显示no x11 display variable was set;
    • ¥15 如何实现这个Python开发问题
    • ¥15 Erasure Code纠删码表
    • ¥15 用vite创建的vue3项目,404重定向不起作用??
    • ¥15 关于#c语言#的问题:一个球从80米高度自由落下,每次落地后反弹的高度为原高度的一半计算6次小球反弹的高度.(反弹结果取整,使用走走for循环结构)
    • ¥15 SurfaceControl的screenshot问题
    • ¥15 基于51单片机的oled菜单代码,要C语言,模块化编程!
    • ¥15 JAVAswing,设计一个扑克牌什么的