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.

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

    报告相同问题?

    悬赏问题

    • ¥15 深海控制器DSE7320MKII和博世ECU间can通讯知识
    • ¥15 Ru的复折射率用于FDTD 200nm到1200nm
    • ¥15 使用Fiddler抓包,textview的乱码如何解决
    • ¥50 trio连接驱动器报错
    • ¥15 有谁懂nhanes的权重计算啊
    • ¥15 欧姆龙PLC 电机控制 限位
    • ¥30 如何处理shell命令接收到的视频流并实时播放出来
    • ¥15 虚心请教C#的代码优化问题
    • ¥15 有偿求做台风过境全过程模拟仿真
    • ¥50 求!AutomationDesk 如何自动导入Variant数据