简体   繁体   中英

php mysqli, how to select values and prepare them to update other tables

I have a mysqli query with the following code:

    $stmt = $mysqli->prepare('SELECT `user`, `status` FROM `mytable1` WHERE `type`=?');
    $type = 1;
    $stmt->bind_param('i', $type);
    $stmt->execute();
    $stmt->bind_result($user, $status);
    $stmt->store_result();
    $stmt->fetch();
    $stmt->close();

And I need update column "Money" where users have type "1" from "mytable1"

$stmt = $mysqli->prepare('UPDATE `mytable2` SET `money`=100 WHERE `user`=?');
$stmt->bind_param('s', $user);
$stmt->execute();
$stmt->close();

In this way update only one user. I found PHP documentation but there is no example :(

Perhaps a combined query like this:

UPDATE `mytable2` SET `money`=100 WHERE `user` in (
SELECT distinct `user` FROM `mytable1` WHERE `type`=?'
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM