sqlstate[42s22]:在restapi中找不到列

unftdfkk  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(295)

我可以显示所有结果,但当我调整代码以尝试更具体地查询时,它返回42s22{“error”:{“text”:sqlstate[42s22]:column not found:1054 unknown column'plant'in'where clause'}即使有一个具有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:我试着用plant过滤结果

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、1);

1hdlvixo

1hdlvixo1#

多亏了埃德·科特雷尔,我找到了答案。我把它贴在这里以防万一有人也需要答案。分享就是关心,我想

<?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;
        }
    }
    ?>

相关问题