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

这是我的代码

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;
}


以下快照是我的数据库:

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

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

最佳答案

主要问题在这里:

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


应该是这样,我猜:

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


否则查询将导致类似

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

关于php - 致命错误:消息为'SQLSTATE [42S22]的未捕获异常'PDOException':找不到列:1054 display.php第44行中的未知列1,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43224673/

10-10 17:30