dongqian1925 2014-03-20 11:11
浏览 28

php没有执行格式正确的posqtgresql语句

i have this code:

$dbconn = pg_connect("host=localhost dbname=dbbase user=postgres password=postgres") or die('Connection error: ' . pg_last_error());
$qstation ="SELECT id FROM st WHERE string ='".$string."'";
$rstation = pg_query($dbconn,$qstation) or die('Query error: ' . pg_last_error());

echo $qstation;

this shows me

SELECT id FROM st WHERE string='A_AA_00_00_A'

if i execute it on pgadmin3, it returns me the result.

but if i try to do

print_r(pg_fetch_array($rstation, null, PGSQL_ASSOC));

i have no erros and print_r() does not return me anything.

if i change the $string variable with 'A_AA_00_00_A'

$qstation ="SELECT id FROM st WHERE string='A_AA_00_00_A'";

the sentence executes right and print_r() returns data

Array ( [id] => 10 )

what am i doing wrong?

  • 写回答

1条回答 默认 最新

  • dongwei3172 2014-03-20 13:56
    关注

    You really should be using parameterized statements here, though I see no reason to think that's the problem in this specific case. Use PDO, or pg_query_params. See http://bobby-tables.com, google "SQL injection", PHP manual on SQL injection.

    In a case like this you really need to look at the PostgreSQL server error logs to determine whether the query is reaching PostgreSQL at all, and if it is, whether it's failing with an error.

    You also appear to be failing to properly check for errors in your PHP code. Use pg_result_status to test for an error, and pg_result_status to get error specifics.

    $rstation = pg_query($dbconn,$qstation) or die('Query error: ' . pg_last_error());
    if (pg_result_status($rstation) != PGSQL_TUPLES_OK) {
        $errmsg = pg_result_error($rstation);
        # ... report error to user or handle it ...
    }
    

    You might also want to look at pg_result_error_field for specific error fields.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c