i am trying to UPDATE multiple rows with mysql. I know how to do it with multiple queries but i think it would be less resource consuming generating mysql query code with php and update all one single step.
here is the method i usually employ:
$value_column_1 = array();
$value_column_2 = array();
Expand|Select|Wrap|Line Numbers
- for($i = 0; $i<= $number_of_updates; $i++){
- $query = "UPDATE table SET column_1 = '$value_column_1[$i]' WHERE
- column_2 = '$value_column_2[$i]'";
- }
but i think it would be more efficient to make a query like the ones that can be made with
Expand|Select|Wrap|Line Numbers
- INSERT INTO table(column_1, column_2) VALUES ($value_column_1[$i], $value_column_2[$i])
making a "big" query:
Expand|Select|Wrap|Line Numbers
- $first_time = true; //first time is set to determine weather to put a ', ' or not
- $q = "INSERT INTO table(column_1, column_2) VALUES";
- for($i = 0; $i<= $number_of_updates; $i++){
- if($first_time == true){
- $q = $q."($value_column_1[$i], $value_column_2[$i])";
- $first_time = false;
- }
- else{
- $q = $q.",($value_column_1[$i], $value_column_2[$i])";
- }
- }
- is there some thing similar with update?
- thank you