Php SQLSTATE[42S22]:在REST Api中找不到列

Php SQLSTATE[42S22]:在REST Api中找不到列,php,mysql,sql,rest,Php,Mysql,Sql,Rest,我可以显示所有结果,但当我调整代码以尝试更具体地查询时,它返回42s22 {“error”:{“text”:SQLSTATE[42S22]:未找到列:“where子句”中的1054未知列“Plant”}即使有一列具有Plant值,它也会显示此错误 <?php require '.././libs/Slim/Slim.php'; require '.././include/db.php'; \Slim\Slim::registerAutoloader(); $app = new \Sl

我可以显示所有结果,但当我调整代码以尝试更具体地查询时,它返回42s22 {“error”:{“text”:SQLSTATE[42S22]:未找到列:“where子句”中的1054未知列“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();
?>
例如,如果我使用/location/plant/Bawang 我想检索id为3和4的条目

(3"霸王","14.3975636","121.0447081",,
(4,'Bawang','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();
?>

您对以下内容非常开放。您需要使用准备好的语句,而不是将变量连接到查询中。仅转义变量是不够的。请参阅。因此,当您浏览到类似于
/location/plant/Bawang
的内容时,您会发现错误吗?@EdCottrell谢谢!刚刚解决了这个问题。在下面发布了我的解决方案,以防万一someone也会遇到同样的问题我刚刚意识到问题是您在原始问题的查询中忽略了在引号中添加
$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{

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