How to store an Array to MySQL from PHP with minimum execution time -


i have store mysql php quite lot; content stored have in arrays. have written small set of helper functions saved me lot of time.

this main function store stores $array mysql $table:

function store($array, $table, $limit = 1000, $mode = '') {      if (empty($array)) return false;      $sql_insert = "insert `".$table."` (";      $array_keys = array_keys($array);      $keys = array_keys($array[$array_keys[0]]);      foreach ($keys $key) $sql_insert .= "`".$key."`,";      $sql_insert = replaceend($sql_insert, ') values ', 1);      $count = 1;      $sql = $sql_insert;      foreach ($array $array_num => $row) {          $sql .= '(';          foreach ($row $key => $value) {              $string = $value;             if (!is_null($value)) {                 $sql .= "'".str_replace("'", "\'", $string)."',";             } else {                 $sql .= "null,";             }          }          $sql = replaceend($sql, '),', 1);          $count = checkinsert($sql, $count, $limit);         if ($count == 0) {             if ($mode == 'print') echo $sql.'<br />';             $sql = $sql_insert;         }         $count++;      }      $last = lastinsert($sql);     if ($last) {         if ($mode == 'print') echo $sql.'<br />';     }      return true;  } 

i using following helpers within function -

to add correct ending statement strings:

function replaceend($string, $replacer = ';', $number_of_chars = 1) {   return left($string, strlen($string) - $number_of_chars) . $replacer;  } 

to check, if $limit reached lead execution of statement:

function checkinsert($sql, $sqlcount, $sql_limit) {      if ($sqlcount >= $sql_limit) {          $sql = replaceend($sql);          query($sql);          $sqlcount = 0;      }      return $sqlcount;  } 

to check last insert after loop finished:

function lastinsert($sql, $sql_insert = '') {      if (right($sql, 1) != ';') {          if ($sql != $sql_insert) {              $sql = replaceend($sql);              query($sql);              return true;          }      }      return false;  } 

finally, execute statements:

function query($sql) {     $result = mysql_query($sql);     if ($result === false) {         ## error logging     }     return $result; } 

basically, there nothing wrong these functions. there 1 problem, not solve: see, $limit set 1000 default. fine, when comes insert statements content / many characters per line of insert statement, have reduce $limit manually in order avoid errors. question: know way automatically check if current insert statement has reached maximum in order executed correctly without wasting time executing early? solve 2 problems @ once: 1. rid of $limit , 2. minimize execution time needed store array mysql. have not found approach check maximum possible length of string represents mysql insert statement, apply before executing statement...!?


Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -