A problem occurs at work: update a data table.
This table is created by myself and has seven fields: id, name, package, etc.
During creation, because I saved the name and package information in two text files,
So I will use the Insert method to Insert all the names into the database at a time.
All the names are imported into the database, but my package is not imported. In this case, I still want to use the insert method, but not.
In this case, the update method should be used. The idea of updating multiple pieces of information at a time is as follows:
UPDATE table_name SET field_name = CASE other_field WHEN 1 THEN 'value' WHEN 2 THEN 'value' WHEN 3 THEN 'value' end where id IN (1, 2, 3) the test code is as follows: <? Php/** function: insert app's apk, logo_url, document_title, app_desc, package_name * into talbe atable use database db. * // connect database catx. $ server = 'localhost'; $ user = 'root'; $ passwd = 'root'; $ port = '000000'; $ dbname = 'catx '; $ link = mysql_connect ($ server, $ user, $ passwd); if (! $ Link) {die ('could not connect :'. mysql_error ();} else echo "Connected successfully \ n"; mysql_select_db ("db", $ link); // set init variable and start time $ st = microtime_float (); $ table = "pydot_g"; $ path = "txt"; $ fname_package_name = "package_name.txt"; // $ handle = @ fopen ($ path. "/". $ fname_package_name, "r"); $ I = 1; $ SQL = "UPDATE pydot_g SET package_name = CASE id"; $ ids = ""; while ($ buf [$ I] = fgets ($ Handle, 512 ))! = False) {$ SQL. = sprintf ("WHEN % d THEN '% S'", $ I, $ buf [$ I]); // concatenate an SQL statement $ ids. = sprintf ("% d,", $ I); $ I ++;} // $ ids = implode (',', $ ids); $ ids. = $ I; $ SQL. = "end where id IN ($ ids)"; echo $ SQL; mysql_query ($ SQL); fclose ($ handle); mysql_close ($ link ); // echo the results and total time used $ et = microtime_float (); $ t = $ et-$ st; echo "\ r \ ninsert into talbe", $ table, "", $ I, "times; \ r \ n"; echo "Total time $ t sec Onds. \ r \ n "; // function calculate time, return a float number function microtime_float () {list ($ usec, $ sec) = explode (" ", microtime ()); return (float) $ usec + (float) $ sec) ;}?>