1

how do i update multiple row with one query?

table
id   ref_no   name
-------------------
1              a
2              b
3              c
4              e
5              f
...................

since i just created new ref_no column and its blank. and i expect the column id = ref_no

i tried below but giving me same ids:

$q = $db->query("SELECT id_user FROM user");
while($r = $q->fetch_array(MYSQLI_ASSOC)) :
    $db->query("UPDATE user SET user_no='".$r['id']."'");
endwhile;

5 Answers 5

3

You can do it with mysql only

UPDATE table_name SET ref_no = id
Sign up to request clarification or add additional context in comments.

2 Comments

is it possible like to append ref in front it? ie: ref1, ref2, etc. $db->query("UPDATE user SET ref_no=AE'".$r['id']."' where id=".$r['id']); not sure it would work?
you can do that UPDATE table_name SET ref_no = CONCAT('ref',id)
2

Do you want every row to have the same value in the ref_no columan as in the id column? In this case you can use

UPDATE user SET ref_no = id

but I'm not sure if that's what you're looking for...

Comments

1
UPDATE user
SET ref_no = id
WHERE ref_no IS NULL

Be aware that, in SQL context, blank is a special concept that represented by the NULL keyword. You have to use IS NULL or IS NOT NULL to test against NULL.

Comments

1

I dont think the other answers is what you want. If you need the id of each row to be equal to the ref:

$q = $db->query("SELECT id_user FROM user");
while($r = $q->fetch_array(MYSQLI_ASSOC)) :
    $db->query("UPDATE user SET ref_no='".$r['id']."' where id=".$r['id']);
endwhile;

Comments

0

Simply:

UPDATE user SET ref_no = id

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.