Ask for a piece of code execution result output code written

Source: Internet
Author: User
The following is a PHP code, the main function is to manipulate the database, but based on the $new_data Array (array 1) for database operations, so every time the database operations
The following is the contents of the $new_data Array (array 1) file.

The result is that when the program loop executes, it can be judged that the operation database failed and which one was successful. The program executes multiple database operations within the loop and outputs the output to all successful {"OK": true}, complete failure {"OK": false} Partial failure (verbose output site such as {"Del": false, "Del_err": $u _id| $tasw _id, "up ": false," Up_err ": $u _id| $tasw _id}
Note that the loop will perform multiple deletions, updates, insert database operations, and of course rewrite the code, but the array format is fixed, $new the _data Array (array 1) executes to $u_id= $res [' u_id ']; generates the following $res array (array 2) for easy database operation

foreach ($new _data as $key) {$res = array (); foreach ($key as $itm) {foreach ($itm as $it = $valu) {$res [Shortstr2 ($it)] = $v                Alu        }} $u _id= $res [' u_id '];        $res [' tasw_id ']= $tasw _id;                if ($tasw _p = = ' x ') {//performs the delete operation $TASW _u_id=substr ($e [0],1,strlen ($e [0]);                if (!db_del ($tasw _id, $TASW _u_id)) {$tasw _err[' d_err ']=false;                }else{$tasw _err[' D_ok ']=true;        }} $chk _u_id= $db->firstvalue ("Select COUNT (*) from {$TBI} WHERE u_id= ' {$u _id} ' and t_id= ' {$tasw _id} '");                if ($chk _u_id>0) {//Update unset ($res [' u_id ']); if (! $db->autoexecute ($TBI, $res, ' UPDATE ', "u_id= ' {$u _id} ' and tree_id= ' {$tasw _id} ')") {$TASW _err[                ' U_err ']=false;              }else{$tasw _err[' U_ok ']=true;  }}else{//Insert if (! $db->autoexecute ($TBI, $res)) {$TASW _err                [' I_err ']=false;                }else{$tasw _err[' I_ok ']=true; }        }}

Array 1

Array ([VLZC9] = = Array ([0] = = Array ([i]/= VLZC9            [^] = asrwe [g] = = m [l] + 12)        [1] = = Array ([i] = VLZC9 [P] = 2)                    ) [FQE10] = = Array ([0] = = Array ([i]/= FQE10 [^] = asrwe [g] = f [s] = VLZC9 [l] = = [1] = = Array ([i] = = FQE10 [P]                    = 1)) [Asrwe] = = Array ([0] = = Array (    [I] = Asrwe [m] = FQE10 [f] = VLZC9))     [Weesa] = = Array   ([0] = = Array ([i] = Weesa [x] = = )        ))

Array 2

Array(    [u_id] => VLZC9    [s_s] => ASRWE    [g] => m    [l] => 12    [p] => 2)Array(    [u_id] => FQE10    [s_s] => ASRWE    [g] => f    [s] => VLZC9    [l] => 12    [p] => 1)Array(    [u_id] => ASRWE    [m] => FQE10    [f] => VLZC9)

Reply content:

The following is a PHP code, the main function is to manipulate the database, but based on the $new_data Array (array 1) for database operations, so every time the database operations
The following is the contents of the $new_data Array (array 1) file.

The result is that when the program loop executes, it can be judged that the operation database failed and which one was successful. The program executes multiple database operations within the loop and outputs the output to all successful {"OK": true}, complete failure {"OK": false} Partial failure (verbose output site such as {"Del": false, "Del_err": $u _id| $tasw _id, "up ": false," Up_err ": $u _id| $tasw _id}
Note that the loop will perform multiple deletions, updates, insert database operations, and of course rewrite the code, but the array format is fixed, $new the _data Array (array 1) executes to $u_id= $res [' u_id ']; generates the following $res array (array 2) for easy database operation

foreach ($new _data as $key) {$res = array (); foreach ($key as $itm) {foreach ($itm as $it = $valu) {$res [Shortstr2 ($it)] = $v                Alu        }} $u _id= $res [' u_id '];        $res [' tasw_id ']= $tasw _id;                if ($tasw _p = = ' x ') {//performs the delete operation $TASW _u_id=substr ($e [0],1,strlen ($e [0]);                if (!db_del ($tasw _id, $TASW _u_id)) {$tasw _err[' d_err ']=false;                }else{$tasw _err[' D_ok ']=true;        }} $chk _u_id= $db->firstvalue ("Select COUNT (*) from {$TBI} WHERE u_id= ' {$u _id} ' and t_id= ' {$tasw _id} '");                if ($chk _u_id>0) {//Update unset ($res [' u_id ']); if (! $db->autoexecute ($TBI, $res, ' UPDATE ', "u_id= ' {$u _id} ' and tree_id= ' {$tasw _id} ')") {$TASW _err[                ' U_err ']=false;              }else{$tasw _err[' U_ok ']=true;  }}else{//Insert if (! $db->autoexecute ($TBI, $res)) {$TASW _err                [' I_err ']=false;                }else{$tasw _err[' I_ok ']=true; }        }}

Array 1

Array ([VLZC9] = = Array ([0] = = Array ([i]/= VLZC9            [^] = asrwe [g] = = m [l] + 12)        [1] = = Array ([i] = VLZC9 [P] = 2)                    ) [FQE10] = = Array ([0] = = Array ([i]/= FQE10 [^] = asrwe [g] = f [s] = VLZC9 [l] = = [1] = = Array ([i] = = FQE10 [P]                    = 1)) [Asrwe] = = Array ([0] = = Array (    [I] = Asrwe [m] = FQE10 [f] = VLZC9))     [Weesa] = = Array   ([0] = = Array ([i] = Weesa [x] = = )        ))

Array 2

Array(    [u_id] => VLZC9    [s_s] => ASRWE    [g] => m    [l] => 12    [p] => 2)Array(    [u_id] => FQE10    [s_s] => ASRWE    [g] => f    [s] => VLZC9    [l] => 12    [p] => 1)Array(    [u_id] => ASRWE    [m] => FQE10    [f] => VLZC9)


   $valu) {$res [Shortstr2 ($it)] = $valu;    }} $u _id = $res [' u_id '];    $res [' tasw_id '] = $TASW _id;    $TASW _err = Array ();                if ($tasw _p = = = ' x ') {//Performs a delete operation $tasw _u_id = substr ($e [0], 1, strlen ($e [0]));            if (!db_del ($tasw _id, $TASW _u_id)) {$tasw _err[' del '] = false;        $TASW _err[' del_err '] = sprintf ('%d|%d ', $u _id, $tasw _id);    }} $chk _u_id= $db->firstvalue ("Select COUNT (*) from {$TBI} WHERE u_id= ' {$u _id} ' and t_id= ' {$tasw _id} '");        if ($chk _u_id>0) {//Update unset ($res [' u_id ']); if (! $db->autoexecute ($TBI, $res, ' UPDATE ', "u_id= ' {$u _id} ' and tree_id= ' {$tasw _id} ')") {$TASW _err[' up ' = FA            Lse        $TASW _err[' u_err '] = sprintf ('%d|%d ', $u _id, $tasw _id);            }}else{//Insert if (! $db->autoexecute ($TBI, $res)) {$TASW _err[' in '] = false;        $TASW _err[' i_err '] = sprintf ('%d|%d ', $u _id, $tasw _id); }} if (!empty ($TASW _erR) {$result [] = $TASW _err;    }}if (Empty ($result)) {$result = array (' OK ' = = True,)} elseif (count ($result) = = = Count ($new _data)) { $result = Array (' OK ' = = False,);} Die (Json_encode ($result));
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.