致命错误:未捕获异常'PDOException',消息'SQLSTATE [42S22]:未找到列:1054 display.php第44行中的未知列1

时间:2017-04-05 07:32:12

标签: php mysql pdo

大家好,我无法解决此错误,我不知道为什么会出现这种错误 我检查了我的数据库并且所有列都存在,我不知道如何解决它,有人可以帮助我。

这是我的代码

class Display extends Journeyholic {

private $tablename;
private $recData = array();

public function __construct($tablename) {

    $this->tablename = $tablename;

    $this->connectToDb();
}

function getAllData() {
    $sql = "SELECT * FROM `$this->tablename` ORDER BY `id` ASC";

    $query = $this->cxn->cxn->prepare($sql);

    $query->execute();
    $data = $query->fetchAll();

    return $data;
}

function getLastRecordDESC() {

    $sql = "SELECT * FROM `$this->tablename` ORDER BY `id` DESC LIMIT 1";
    $query = $this->cxn->cxn->prepare($sql);

    $query->execute();

    $data = $query->fetch();

    return $data;
}

function getRecordByID($id) {

    $id = intval($id);
 $sql = "SELECT * FROM `$this->tablename` WHERE `$id`=$id";
  $query = $this->cxn->cxn->prepare($sql);

    $query->execute();

    $this->recData = $query->fetch();

    return $this->recData;

}


function getAllDataByID($id, $column = "id") {

    $id = intval($id);
    $sql = "SELECT * FROM `$this->tablename` WHERE `$column`=$id";
    $query = $this->cxn->cxn->prepare($sql);

    $query->execute();

    $data = $query->fetchAll();

    return $data;
}

以下是我的数据库:

Database

我也尝试将反引号更改为单引号,但它不起作用。我只是需要有人告诉我什么是错误的以及如何解决它?

1 个答案:

答案 0 :(得分:2)

主要问题在于:

"SELECT * FROM `$this->tablename` WHERE `$id`=$id"

应该这样我猜:

"SELECT * FROM `$this->tablename` WHERE `id`= " . $id

否则查询会产生类似

的内容
"SELECT * FROM `$this->tablename` WHERE `12345`= 12345