php SQLSTATE[42S22]:未找到列:1054 未知列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10129120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SQLSTATE[42S22]: Column not found: 1054 Unknown column
提问by Darren Burgess
I am attempting to insert a record to MySQL using PDO, my sql statement can be seen in the following code.
我正在尝试使用 PDO 向 MySQL 插入一条记录,我的 sql 语句可以在以下代码中看到。
<?php
try{
//include file myfunctions.php allows us to calls functions from it
include ("myfunctions.php");
//Assign function getConnection() from myfunctions.php to variable $db
$db = getConnection();
foreach($_POST['chk'] as $check_value)
{
$check = $check_value;
$fav = "channel/item [title = \"$check\"]";
$holidayDoc = simplexml_load_file('holidays.xml');
$favourites = $holidayDoc->xpath($fav);
foreach($favourites as $currentFav)
{
echo "{$currentFav->link}". "<br \>";
echo "{$currentFav->title}". "<br \>";
echo "{$currentFav->description}". "<br \>";
echo "{$currentFav->pubDate} ". "<br \>";
$sql = "INSERT INTO `saved_holidays` (`subscriberID`, `link`, `pubDate`, `title`, `description`)
VALUES (`John`, `$currentFav->link`, `$currentFav->pubDate`, `$currentFav->title`, `$currentFav->description`)";
$db->exec($sql);
$db = null;
}
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
When this code is executed i am met with the following error message;
执行此代码时,我遇到以下错误消息;
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'John' in 'field list'
SQLSTATE[42S22]:未找到列:1054“字段列表”中的未知列“John”
This is no doubt a simple solution to this problem but i cannot seem to see it, can anyone point me in the right direction?
这无疑是这个问题的简单解决方案,但我似乎看不到它,有人能指出我正确的方向吗?
回答by Justin Pihony
I believe this is because you are using backticks for your values. Change them to single quotes and you should be good
我相信这是因为您正在为您的价值观使用反引号。将它们更改为单引号,您应该会很好
$sql = "INSERT INTO `saved_holidays` (`subscriberID`, `link`, `pubDate`, `title`, `description`)
VALUES ('John', '$currentFav->link', '$currentFav->pubDate', '$currentFav->title', '$currentFav->description')";
Please refer to this SO question about single quotes versus backticksif you want more information
如果您需要更多信息,请参阅有关单引号与反引号的 SO 问题
回答by Pierre-Olivier
` is for specifying fields, you must use a single quote 'for values.
` 用于指定字段,您必须'对值使用单引号。
$sql = "INSERT INTO `saved_holidays` (`subscriberID`, `link`, `pubDate`, `title`, `description`)
VALUES ('John', '$currentFav->link', '$currentFav->pubDate', '$currentFav->title', '$currentFav->description')";
回答by sujal
$sql = "INSERT INTO `saved_holidays` (`subscriberID`, `link`, `pubDate`, `title`, `description`)
VALUES ('John', '$currentFav->link', '$currentFav->pubDate', '$currentFav->title', '$currentFav->description')";
Use ` for fields only and use ' for values
仅对字段使用 ` ,对值使用 '
回答by Lilly_Code
For anyone else struggling with this, this is why I have seen this error message and how I solved it:
对于其他为此而苦苦挣扎的人,这就是我看到此错误消息以及我如何解决它的原因:
- I was working on a shared code base (in work so many contributors)
- Someone else working on the code added a column without you knowing.
- I pulled the code, all was working yesterday but today I got the error message: "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'column_name' in 'field list' "
- 我正在开发一个共享代码库(在工作中有很多贡献者)
- 其他处理代码的人在您不知情的情况下添加了一个列。
- 我拉了代码,昨天一切正常,但今天我收到错误消息:“SQLSTATE [42S22]:未找到列:1054 '字段列表'中的未知列'column_name'”
All you have to do is go to your table and add the Column (for me it was in phpmyadmin that I did this).
您所要做的就是转到您的表并添加列(对我而言,我是在 phpmyadmin 中执行此操作的)。
From the error message I received, I could deduce it was 'properties' table that needed the additional column pricing_formula. Just insert this column directly into the DB.
从我收到的错误消息中,我可以推断出需要附加列pricing_formula 的是'properties' 表。只需将此列直接插入数据库即可。
For reference, this was my error message:
作为参考,这是我的错误消息:
"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'pricing_formula' in
'field list' (SQL: select `site`.`name`, `domain`, `site`.`uuid`,
`pricing_formula`, `client`.`uuid` as `clientRef` from `site` inner join
`client` on `site`.`client_id` = `client`.`id` where `client_id` = 1 and
`site`.`deleted_at` is null)"
Hope this helps someone. If anyone still doesn't fully know how to fix this issue but thinks my explanation describes their problem, reach out - I will try to help you fix it.
希望这可以帮助某人。如果有人仍然不完全知道如何解决这个问题,但认为我的解释描述了他们的问题,请联系 - 我会尽力帮助你解决它。
Also for info, this was a Laravel backend project with an Angular front end.
同样作为信息,这是一个带有 Angular 前端的 Laravel 后端项目。
回答by Mohammed_7aafar
Update your model with this:
用这个更新你的模型:
public $timestamps = false;
This happened because Eloquent function adds 'updated at' property to your query which is timestamp and this will throw this error.
发生这种情况是因为 Eloquent 函数将 'updated at' 属性添加到您的查询中,即时间戳,这将引发此错误。

