在Postgresql查询中包含一个变量

时间:2016-03-01 00:46:14

标签: php postgresql

我从使用Postgresql的API获取数据。以下不起作用。我回来了{error: ["syntax error at or near "0""]}

$selected_column ="some_column"; //or a `POST` (e.g.,`$_POST['my_column'];`)        
$service_url = 'https://something.html?';
       $curl = curl_init($service_url);
       $curl_post_data = array(
            "q" => "SELECT something, another, thing, rank() OVER ( ORDER BY " + $selected_column + " DESC)  from my_database",
            "api_key" => 'XXXYYYZZZ',
            );
       curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
       curl_setopt($curl, CURLOPT_POST, true);
       curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
       $curl_response = curl_exec($curl);
     echo $curl_response;

但是,如果我将列值从$selected_column"硬编码"进入查询,它工作正常。也就是说,只需改变这一行:

 "q" => "SELECT something, another, thing, rank() OVER ( ORDER BY some_column DESC)  from my_database",

我猜我不能通过这样的变量,但我不知道你怎么做。

感谢。

0 个答案:

没有答案