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 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效