2014-07-21 23:09 阅读 39


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 = '';

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


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


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

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 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 = '';
    $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.

    点赞 评论 复制链接分享