slim api不输出数据

时间:2018-04-07 07:57:00

标签: php api ionic-framework slim

我用邮递员测试了api路线并且它给出了状态200消息,但它没有输出数据....请帮忙。提前谢谢

$app->get('/getproducts', 'getproducts');

function getproducts(){

$query = "SELECT * FROM product where p_available = 1 ";

try {

        $db = getDB();
        $stmt = $db->query($query);
        $stmt->execute();
        $product = $stmt->fetchAll(PDO::FETCH_OBJ);
        $db = null;
        // echo '{"product": '. json_encode($product) .'}';
         return '{"product": ' . json_encode($product) . '}';

} catch(PDOException $e) {
    echo '{"error":{"text":'. $e->getMessage() .'}}';
} 
}

1 个答案:

答案 0 :(得分:-1)

use prepare before execute if its not work plz check that are you use getInstance()->request(); in your page ?

    $app->get('/getproducts', 'getproducts');

function getproducts(){

$request = \Slim\Slim::getInstance()->request();
$dataInfo = json_decode($request->getBody());

    $query = "SELECT * FROM product where p_available = 1 ";

    try {

            $db = getDB();
            $stmt = $db->query($query);
            $stmt = $db->prepare($query);   
            $stmt->execute();
            $product = $stmt->fetchAll(PDO::FETCH_OBJ);
            $db = null;
            // echo '{"product": '. json_encode($product) .'}';
             return '{"product": ' . json_encode($product) . '}';

    } catch(PDOException $e) {
        echo '{"error":{"text":'. $e->getMessage() .'}}';
    } 
    }