[英]PDO #1054 Unknown column 'n' in 'where clause
I have a class with a function that will pull out any data from the database whatever the identifying column may be, which basicly looks like this 我有一个带有函数的类,无论标识列可能是什么,该函数都会从数据库中提取任何数据,基本上看起来像这样
use Database\DB;
class General extends DB
{
private $fooBar;
public function getData($column, $table, $value) {
$column = (array) $column;
$column = implode(', ', $column);
$test = $this->query("SELECT `$column` FROM `$table` WHERE $column[0] = :value", array("value" => $value));
}
}
and is executed as following: 并执行如下:
return $this->general->getData(['name'], 'people', 'John Anderson');
However, I'm getting an error which tells me there is an non-existent column input with the value 'n' (which is happening to be the first character of the column name, whatever the column name's value is) 但是,我收到一条错误消息,告诉我不存在具有值'n'的列输入(无论该列名称的值是什么,该输入正好是列名称的第一个字符)
Full error; 完全错误;
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'n' in 'where clause'' in a\long\path\DB.class.php on line 50
PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'n' in 'where clause' in a\long\path\DB.class.php on line 50
Thanks in advance, Jordi 在此先感谢,乔迪
Get rid of the line 摆脱界限
$column = implode(', ', $column);
It's replacing the array with a string containing all the column names separated by comma. 它用包含用逗号分隔的所有列名称的字符串替换数组。 Then
$column[0]
will be the first character of the first column name, instead of the first column name. 然后
$column[0]
将是第一个列名的第一个字符,而不是第一个列名。
Put down your full query here.. Than it's easier! 在此处输入您的完整查询。
Edit: 编辑:
try this code: 试试这个代码:
use Database\DB;
class General extends DB
{
private $fooBar;
public function getData($column, $table, $value) {
$columns = (array) $column;
$column = implode(', ', $columns);
$test = $this->query("SELECT `$column` FROM `$table` WHERE $columns[0] = :value", array("value" => $value));
}
}
I changed the following: $column = (array) $column;
我更改了以下内容:
$column = (array) $column;
to $columns = (array) $column;
到
$columns = (array) $column;
And 和
SELECT `$column` FROM `$table` WHERE $column[0] = :value", array("value" => $value)
to 至
SELECT `$column` FROM `$table` WHERE $columns[0] = :value", array("value" => $value)
This should fix it (see @Barmar answer as well): 这应该可以解决(也请参见@Barmar答案):
(Take note of the difference in the implode statement as well). (还要注意内爆声明中的差异)。
use Database\DB;
class General extends DB
{
private $fooBar;
public function getData($column, $table, $value) {
$column = (array) $column;
$whereCol = $column[0];
$column = implode('`, `', $column);
$test = $this->query("SELECT `$column` FROM `$table` WHERE $whereCol = :value", array("value" => $value));
return $test;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.