-1

当我执行这个方法时,数据库返回如下错误: 1. 数据库没有问题,检查所有列名都匹配。代码在语法上也是正确的。我有另一种实现相同的方法,只是表名和列名不同。

Fatal error: Call to a member function execute() on a non-object in C:\xampp\htdocs\timetable-generator-v2\inout.php on line 173


$db = new Database();
$db->insertRule("something", NULL, NULL);


class Database{
    private $conn;
    function __construct() {
        try {
            $this->conn = new PDO("sqlite:" . DATABASE);
        } catch (PDOException $e) {
            echo $e->getMessage() . " - failed on: Database->__construct()";
            die();
        }

    }
    function insertRule($ruleName, $isNot, $isInverse){
        $ruleId = "ru1";//$this->generateId(RULE);
        try {
            $q = $this->conn->prepare("INSERT INTO Rule(ruleId, ruleName, not, inverse) VALUES(?, ?, ?, ?)");
            $q->execute(array($ruleId, $ruleName, $isNot, $isInverse));
        } catch (PDOException $e) {
            echo $e->getMessage() . " - failed on: Database->insertRule()";
            die();
        }
    }
}
4

2 回答 2

1

NOT是一个关键字。用于"not"将其用作列名。

于 2013-03-18T08:00:26.103 回答
0

一个适当的代码让自己意识到错误

class Database{
    private $conn;
    function __construct() {
        $opt = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
        $this->conn = new PDO("sqlite:" . DATABASE,NULL,NULL,$opt);
    }
    function insertRule($ruleName, $isNot, $isInverse){
        $ruleId = "ru1";//$this->generateId(RULE);
        $q = $this->conn->prepare("INSERT INTO Rule(`ruleId`, `ruleName`, `not`, `inverse`) VALUES(?, ?, ?, ?)");
        $q->execute(array($ruleId, $ruleName, $isNot, $isInverse));
    }
}

尽管任何特定的 insertRule 都不应该在 Database 类中

于 2013-03-18T08:11:49.383 回答