使用Slim框架和MSSQL数据库的Restful API

时间:2016-05-28 01:33:30

标签: php sql-server api slim

我的api与mySql运行良好但是当我换成MSSQL时出现错误。 实际上我已经从MSSQL获得了所有数据,但我的其他api没有用。 这是我获取所有数据的工作代码:

$app->get('/mirror', 'getAll'); 
$app->get('/mirror/{skincare_process_info_id}', function($request,$response,$args){
    get_skin_id($args['skincare_process_info_id']);
}); 
$app->run();

function getAll() {
    $sql = "select * FROM SkincareProcessInfo ORDER BY skincare_process_info_id";
    try {
        $db = connect_db();
        $stmt   = $db->query($sql);
        $skin  = $stmt->fetchAll(PDO::FETCH_OBJ);
        $db = null;
        echo '{"skin process info": ' . json_encode($skin) . '}';
    }catch(PDOException $e) {
        echo '{"error":{"text":'. $e->getMessage() .'}}';
    }    
} 

它工作正常,但在我的其他api's: 这是我通过id获取数据的api:

    function get_skin_id($skin_id){

    $sql = "SELECT * FROM SkincareProcessInfo WHERE `skincare_process_info_id` = '$skin_id'";

     try{ 
        $db = connect_db();
        $stmt = $db->query($sql);
        $skin = $stmt->fetchAll(PDO::FETCH_ASSOC);
        $db = null; 
         if($stmt == 1){
            echo '{"skin process info": ' . json_encode($skin) . '}';
        }else{
            echo 'false';
        } 

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

这是我得到的错误:

Fatal error: Call to a member function fetchAll() on boolean in C:\xampp\htdocs\SLIM\mirror.php on line 57

1 个答案:

答案 0 :(得分:1)

试试这个

$db = connect_db();
$query = $db->prepare("SELECT * FROM SkincareProcessInfo WHERE skincare_process_info_id = ?");
$query->execute(array($skin_id));
$fetch = $query->fetchAll(PDO::FETCH_ASSOC);
$rowCount = $query->rowCount();
if($rowCount > 0){
echo '{"skin process info": ' . json_encode($fetch) . '}';
}else{
echo 'false';
}