php - rtrim fails with very long strings -


i'm building large query string, (8000 raws, using once in million years.) need rid of final comma, added while string being built.

take @ code, reason if rtrim() nothing it; string passed rtrim() returns intact.

(if need copy of value of query_string value, let me know , i'll upload too.)

public function sync_sites_table() {      # $query_string = "truncate table " . self::$table_name;     # $db->query($query_string);      $this->build_sites_object();      $query_string = "insert `".self::$table_name . "` (`site_id`, `site_name`) values \n";      foreach($this->sites_array $key => $value)     {         $query_string .= "('".$key."','".$value."'), \n";     }      rtrim($query_string, ",");      global $db;     $db->query($query_string);  } 

rtrim() isn't pass reference; returns trimmed value need assign variable:

$query_string = rtrim($query_string, ","); 

but cleaner answer might be:

$first = true; foreach($this->sites_array $key => $value) {     if (!$first) { $query_string .= ', '; } else { $first = false; }     $query_string .= "('".$key."','".$value."') \n"; } 

so wouldn't need trim trailing ,


Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -