Friday 15 July 2011

mysql - How to get a loop query into one query in php -


I get something like this.

This is working, but doing a MySQL query 40 times is not the best thing.

Can anyone help me by taking it in a query? Exclusive string '; $ Number = count ($ some); ($ (= $ Some_input)) for $ (= $ i = 0; $ i & lt; $ number; $ i ++) {$ string_one = 'something' $ I; $ String_two = 'some _ two' Enter the values ​​of duplicate at $ I; Mysql_query ("Insert values" ('$ unique_string', '$ string_one', '$ string_two') in `table` (` unique_string`, 'string_on', `string_to`)` string_one` = '$ String_one', `String_two` = '$ string_two'") or die (mysql_error ());} and {$ string_one = '' $ String_two = ''; Mysql_query ("` table` (`unique_string`, Enter the values ​​in the string ('$ unique_string', '$ string_one', '$ string_two') in duplicate key update `string_on 'in' string_on ',` string_to`) E` = '$ string_one', `String_two` = '$ string_two'") or die (mysql_error ());}

You can generate a single query in that loop and then execute it only once:

  $ query = '` table` ( 'INSERT' value in `unique_string`,` string_on`, `string_to`); $ QueryValues ​​= array (); $ String_unique = 'Unique String'; $ Number = count ($ some); ($ (= $ Some_input)) for $ (= $ i = 0; $ i & lt; $ number; $ i ++) {$ string_one = 'something' $ I; $ String_two = 'some _ two' $ I; $ QuillValues ​​[] = sprintf ('("% s", "% s", "% s"), $ unique_string, $ string_one, $ string_two);} Else {// I do not understand Coming to whom you are using this part ... anyway. $ QueryValues ​​[] = sprintf ('("% s", "", "")', $ unique_string);}} $ Query. = Implode (',', $ queryValues); // and there is a unique key update // I do not know the whole structure, so I think you have the primary `id` line // What was unnecessary on your on-deployment key was. $ Query. = 'Key update of duplicates' ID` = `ID`'; // Do not use mysql_ * function in new code: // see: http: // stackoverflow .com / questions / 12859942 / why-shouldnt-i-use-mysql-functions-in-php mysql_query ($ query);    

No comments:

Post a Comment