PHP PDO SQLite准备的语句问题 [英] PHP PDO SQLite prepared statement issues

查看:49
本文介绍了PHP PDO SQLite准备的语句问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将PHP应用程序从MySQL迁移到SQLite,并且某些曾经起作用的东西现在只是停止工作了.我通过自定义数据库包装器类使用PDO(该类是单例,这样做似乎很合逻辑).

I am trying to migrate a PHP app from MySQL to SQLite, and some things that used to work, simply stopped working now. I am using PDO through a custom database wrapper class (the class is a singleton, seems logical to do it like that).

问题: 尝试对准备好的语句执行查询时,它引发致命错误:在非对象上调用成员函数execute()...".

The problem: When trying to execute a query on a prepared statement, it throws a "fatal error: Call to a member function execute() on a non-object ...".

相关代码(经过几个小时的var_dumps和try-catch之后,将其范围缩小了):

Relevant code (narrowed it down to this, after some hours of var_dumps and try-catch):

连接字符串:

$this->connection = new PDO("sqlite:"._ROOT."/Storage/_sqlite/satori.sdb");

很明显,这里的$ connection变量是该类的私有变量.

Obviously, the $connection variable here is a private variable from the class.

错误在这里发生(在应该执行数据库插入的函数内部):

The error happens here (inside a function that is supposed to perform database insert):

    try{
        $statement = self::getInstance()->connection->prepare($sql);
    }catch (PDOException $e){
        print $e->getMessage;
    }

    try{
        var_dump($statement);
        $statement->execute($input);
    }catch (Exception $e){
        print $e->getMessage();
    }

更准确地说,是在我尝试$ statement-> execute($ input)时发生的.

More accurately, it happens when I try to $statement->execute($input).

任何帮助表示赞赏.谢谢.

Any help appreciated. Thanks.

推荐答案

正在准备语句时,您可能会遇到mySQL错误,但是您没有将PDO配置为

You are probably getting a mySQL error when the statement is being prepared, but you don't have PDO configured to throw an exception in case of an error.

<?php

  $dbh = new PDO( /* your connection string */ );
  $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
  // . . .
?>

这篇关于PHP PDO SQLite准备的语句问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆