juzi
V2EX  ›  问与答

php 盲请教这个语句是怎么改 php 执行 sql 语句

  •  
  •   juzi · Oct 16, 2015 · 2905 views
    This topic created in 3888 days ago, the information mentioned may be changed or developed.
    UPDATE `user` SET `plan` = 'free',`u` = '0',`d` = '0',`transfer_enable` = '300' WHERE `user`.`u` + `d` >= 'transfer_enable' AND `plan` = 'pro'

    大概意思就是当条件成立把 plan 为 pro 的变成 free , sql 自己可以摸索着这样写

    但是 php 这样写

    <?php
    require_once '../lib/config.php';

    $info = $db -> select('user',"*",[
    "plan" => 'pro'
    ]);

    for($i=0;$i<count($info);$i++){
    if($info[$i]['u'] + $info[$i]['d'] >= $info[$i]['transfer_enable']){
    $db -> update('user',[
    "u"=>0,
    "d"=>0,
    "transfer_enable"=>1073741824,
    "plan"=>'free'
    ]);
    }
    }

    执行完了就把全部表都改了,我知道要添加 where 条件,不知如何修改,请不吝赐教。
    4 replies    2015-10-17 14:34:46 +08:00
    gdtv
        1
    gdtv  
       Oct 17, 2015   ❤️ 1
    30 分钟过去了没人回答,难道这道题有什么坑?
    因为你$db -> update 的时候没有 where 条件,所以全都改了。
    我不知你的$db 对象是怎样封装的,猜测大概可以这样写:

    require_once '../lib/config.php';
    $db -> query("UPDATE `user` SET `plan` = 'free',`u` = '0',`d` = '0',`transfer_enable` = '300' WHERE `user`.`u` + `d` >= 'transfer_enable' AND `plan` = 'pro' ");
    juzi
        2
    juzi  
    OP
       Oct 17, 2015 via Android
    @gdtv 因为大家都睡觉了 233
    dsphper
        3
    dsphper  
       Oct 17, 2015   ❤️ 1
    链式 db 根据推测可以这么写:
    $db -> update('user',[
    "u"=>0,
    "d"=>0,
    "transfer_enable"=>1073741824,
    "plan"=>'free'
    ])->where(['id'=>123]) || where('id=123');
    juzi
        4
    juzi  
    OP
       Oct 17, 2015 via Android
    @dsphper 效果达到要求
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3224 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 13:28 · PVG 21:28 · LAX 06:28 · JFK 09:28
    ♥ Do have faith in what you're doing.