duanliaozhi2915 2014-07-21 23:09
浏览 44
已采纳

PHP无法在Postgres数据库中增加价值

I could use some help understanding an odd situation. I have a postgres database 'servers' with ip (inet) and load (integer) columns. I can manually update a load value via psql as follows:

UPDATE servers 
    SET load = load + 100 
WHERE ip = '10.10.10.10';

I have written a php script to automate load updates. However, the 'value = value + x' syntax is not working for me now.

THIS WORKS:

pg_query($conn, "UPDATE servers SET load = 100 WHERE ip = '10.10.10.10'");

THIS DOESN'T:

pg_query($conn, "UPDATE servers SET load = load + 100 WHERE ip = '10.10.10.10'");

When using the latter, the script hangs indefinitely without giving a response. I have tried variations such as adding an or die(pg_last_error()) clause at the end. I have also tried using a pg_prepare and pg_execute statement. Still no output, and no change to the database.

Am I missing something? Is there an easy way to get around this (I'd rather not use a separate query to get load just so that I can add it back in).

  • 写回答

1条回答 默认 最新

  • dongxie2756 2014-07-22 19:43
    关注

    I have solved this issue. I am not certain why this was a problem, but it turns out that best practices helped solve it in the end anyway.

    Instead of running a single pg_query, I set up appropriate variables and used pg_query_params. This is probably a good idea regardless of issues.

    $myQuery = "UPDATE servers SET load = load + $1 WHERE ip = '$2'";
    $serverIP = '10.10.10.10';
    $loadAdded = '1234';
    $result = pg_query_params($conn, $myQuery, array($loadAdded, $serverIP));
    

    Again, I don't know why this solved the issue for me, but it did.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料