0

I have one form for updating a table. When submit the form I want to update 100 rows of one database table. Is it possible with one query? If I use each query for each row in database table it takes a lot of time to update the all data.

$sql="update sd_stu_att2 set attendance='" . $srlz . "' where student_id='" . $student_id . "'";

I want to execute this into 100 times when 100 different students are there. Hence 100 query needs to be executed. Is there one query executes for 100 different student_id in the table?

7
  • Can you post your code which you tried?
    – Sadikhasan
    Jul 23, 2014 at 4:55
  • "If i use each query for each row in databsetable it tooks lot of time to update the all data." - You wouldn't happen to be including your DB connection inside the loop, are you? Usually, that's the most likely issue that will slow down a low-number query, such as 100 or other kinds of useless loops. MySQL can handle those quite fast. Show your code. Jul 23, 2014 at 4:57
  • thanks fred.I changed the DBconnection from the loop.But the sql query run 100 times.Will it slowdown the updation process?
    – Rashid
    Jul 23, 2014 at 5:03
  • 1
    You're welcome. I have seen this before, where someone had the DB connection inside a loop. Once it was placed outside the loop, the speed increased tremendously. Jul 23, 2014 at 5:05
  • Is there any problem running 100 queries on one form submitt??or Is there any other method??
    – Rashid
    Jul 23, 2014 at 5:13

2 Answers 2

1

Yes, UPDATE query can update any number of rows with a single query.

For example, if you have a table with records of students with following schema:

id integer AUTO INCREMENT
name varchar
roll_number integer
class integer

Now, suppose the academic year is over and all the students are promoted to the next class (super simplified example), you can write a single update query to do so, like this:

UPDATE `students` SET `class` = `class` + 1;

No matter how many students you have, all of them will be promoted to the next class with just one single query.

The essence here is that, you have to work with the WHERE clause of the query to update exactly those rows which you want to.

0
0

Yes you can. The syntax of update table is:

UPDATE table_name SET column1=value1,column2=value2,...
WHERE some_column=some_value;
2
  • why You edit my post.
    – karan
    Jul 23, 2014 at 5:21
  • it's very normal here to have one's own posts edited. In this case, it was because you'd rendered ordinary text as formatted code. I've added in some improvements to letter case, punctuation and code margin wrapping.
    – halfer
    Jul 27, 2014 at 10:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.