我有PDO和sqlite3的问题。在尝试连接表时,我收到错误:
object(PDO)#1 (0) { } {"error":true,"message":"SQLSTATE[HY000]: General error: 1 near \u0022FROM\u0022: syntax error"
我的连接和SQL查询看起来像这样。崇高文本2也不像其他人那样着色JOIN
。
$objDb = new PDO('sqlite:../dbase/shopping-list');
var_dump($objDb);
$objDb -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT `i`.*,
`t`.`name` AS `type_name`
FROM `items` `i`
JOIN `types` `t`
ON `t`.`id` = `i`.`type`
ORDER BY `i`.`date` ASC";
$result = $objDb->query($sql);
if(!$result) {
throw new PDOException("The result returned no object");
}
我遵循教程,他可以将此语句与mac一起使用。我使用arch linux,但我不认为这是相关的。
当我用cli检查sqlite数据库时,我可以得到types
表:
sqlite> SELECT * FROM `types`;
1|Qty
2|Kg
无论如何都要完成此查询?
编辑:我已经更改了数据库并删除了反引号...现在我可以获取这些项目,但我仍然有错误,他们在我的angularjs前端内部无法工作。
object(PDOStatement)#3 (1) { ["queryString"]=> string(37) "SELECT * FROM types ORDER BY id" } {"error":false,"items":[{"id":"1","item":"Butter","qty":"1","type":"1","done":"0","date":"2014-10-06 02:45:51","type_name":"Qty"}],"types":[{"id":"1","name":"Qty"},{"id":"2","name":"Kg"}]}
这里是使用JOIN正确的sql查询
$sql = "SELECT item.* ,
t.name AS type_name
FROM items item
INNER JOIN types t
ON t.id = item.type
ORDER BY item.date ASC";
这是我的select.php link