我可以显示所有结果,但是当我调整代码以尝试更具体地查询时,它返回 42s22 {“error”:{“text”:SQLSTATE[42S22]:未找到列:1054“where 子句”中的未知列“Plant”}即使存在包含 plant 值的列,也会显示此错误。
<?php
require '.././libs/Slim/Slim.php';
require '.././include/db.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
$app->get('/location/all', function(){
$sql = "SELECT * FROM plant_location";
try{
$db = new db();
$db = $db->connect();
$stmt = $db->query($sql);
$customers = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
echo json_encode($customers);
} catch(PDOException $e){
echo '{"error": {"text": '.$e->getMessage().'}';
}
});
$app->get('/location/plant/:plant', function($plant){
$sql = "SELECT * FROM plant_location WHERE plant=$plant";
try{
$db = new db();
$db = $db->connect();
$stmt = $db->query($sql);
$customers = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
echo json_encode($customers);
} catch(PDOException $e){
echo '{"error": {"text": '.$e->getMessage().'}';
}
});
$app->run();
?>
SQL: 我试图使用植物过滤结果
CREATE TABLE `plant_location` (
`id` int(11) NOT NULL,
`plant` varchar(255) NOT NULL,
`latitude` varchar(255) NOT NULL,
`longitude` varchar(255) NOT NULL,
`image` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `plant_location` (`id`, `plant`, `latitude`, `longitude`, `image`) VALUES
(1, 'Plant', '14.396033', '121.0452128', '//'),
(2, 'Plant B', '14.3967839', '121.0444142', ''),
(3, 'Bawang', '14.3975636', '121.0447081', ''),
(4, 'Bawang', '14.395036', '121.044177', '');
例如,如果我使用/location/plant/Bawang 我想检索 id 为 3 和 4 的条目
(3, '霸王', '14.3975636', '121.0447081', ''), (4, '霸王', '14.395036', '121.044177', '');
最佳答案
感谢 Ed Cottrell,我找到了答案。我将其发布在这里以防万一有人也需要答案。我想,分享就是关怀
<?php
require '.././libs/Slim/Slim.php';
require '.././include/db.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
$app->get('/location/all', function(){
$sql = "SELECT * FROM plant_location";
try{
// Get DB Object
$db = new db();
// Connect
$db = $db->connect();
$stmt = $db->query($sql);
$customers = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
echo json_encode($customers);
} catch(PDOException $e){
echo '{"error": {"text": '.$e->getMessage().'}';
}
});
$app->get('/location/plant/:plant', function($plant){
// echo 'here';
try{
// echo 'here';
// Get DB Object
$db = new db();
$db = $db->connect();
$stmt = $db->prepare('SELECT * FROM plant_location WHERE plant = :plant');
$stmt->bindValue(':plant', $plant);
$stmt->execute();
$customers = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
echo json_encode($customers);
} catch(PDOException $e){
echo '{"error": {"text": '.$e->getMessage().'}';
}
});
$app->run();
?>
数据库
<?php
class db{
// Properties
private $dbhost = '//myhost';
private $dbuser = '//myuser';
private $dbpass = '//mypass';
private $dbname = '//myname';
// Connect
public function connect(){
$mysql_connect_str = "mysql:host=$this->dbhost;dbname=$this->dbname";
$dbConnection = new PDO($mysql_connect_str, $this->dbuser, $this->dbpass);
$dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $dbConnection;
}
}
?>
关于php - SQLSTATE[42S22] : Column not found in REST Api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51382110/