大家好,我无法解决此错误,我不知道为什么会出现这种错误 我检查了我的数据库并且所有列都存在,我不知道如何解决它,有人可以帮助我。
这是我的代码
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;
}
以下是我的数据库:
我也尝试将反引号更改为单引号,但它不起作用。我只是需要有人告诉我什么是错误的以及如何解决它?
答案 0 :(得分:2)
主要问题在于:
"SELECT * FROM `$this->tablename` WHERE `$id`=$id"
应该这样我猜:
"SELECT * FROM `$this->tablename` WHERE `id`= " . $id
否则查询会产生类似
的内容"SELECT * FROM `$this->tablename` WHERE `12345`= 12345