By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,747 Members | 2,011 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,747 IT Pros & Developers. It's quick & easy.

PHP concatenated string part desaperas

bilibytes
100+
P: 128
Hi,
I am having a problem with a concatenated string.
I start my string outside of a for() and then, concatenate the string generated with the loop to it.

the string out of the loop looks like this:
$q = "INSERT INTO (asdf1,asdf2,asdf3) VALUES"

then when i add something like this:
$q .="('".$asdf1."', '".$asdf2."', '".$asdf3."')"

when i make the query, mysql returns syntax error because the final string looks something like this:

"INSERT INTO (asdf1,asdf2,asdf3) VALUESasdf1', 'asdf2', 'asdf3')asdf1', 'asdf2', 'asdf3')asdf1', 'asdf2', 'asdf3')asdf1', 'asdf2', 'asdf3')...

So the first part of the concatenated string:"('" ,seem to desapear.

I simplified the query string to focus on the real problem...

here is the function code:
Expand|Select|Wrap|Line Numbers
  1. /* Insert new content into calendar */
  2.     private function insert_into_calendar(){
  3.  
  4.         /* Create mysql query code */
  5.         $this->query = 'INSERT INTO calendar (date_id, date, hour, hour_state, hour_sub, hour_sub_state) VALUES';
  6.         $first = true;
  7.         for($i = 1; $i<= $this->days_to_add ; $i++){ //DATE
  8.             $date_to_add = date("d.m.y", mktime(0, 0, 0, $this->table_max_date_exploded['m']  , $this->table_max_date_exploded['d']+$i, "20".$this->table_max_date_exploded['y']));
  9.  
  10.             for($j = Calendar_auto_updater::DAY_HOUR_START; $j <= Calendar_auto_updater::DAY_HOUR_END; $j++){ //HOUR
  11.                 $hour = $j.":00";
  12.  
  13.                 for($k = 0; $k <= $this->hour_sub_max; $k += $this->hour_fraction){ //HOUR_SUB
  14.                     if($k == 0){ //allways want an hour format of this kind "hh:mm"
  15.                         $hour_sub = $j.":".$k."0";
  16.                     }
  17.                     else{
  18.                         $hour_sub = $j.":".$k;
  19.                     }
  20.  
  21.                     if($first == true){ //only want to put a "," before the "(" if its not just after "VALUES" SQL SYNAX
  22.  
  23.                         $this->query .='(\''.$this->table_max_date_id+$i.'\', \''.$date_to_add.'\', \''.$hour.'\', \'0\', \''.$hour_sub.'\',  \'0\')';
  24.                         $first = false;
  25.                     }
  26.                     else{
  27.                         $this->query .=', (\''.$this->table_max_date_id+$i.'\', \''.$date_to_add.'\', \''.$hour.'\', \'0\', \''.$hour_sub.'\', \'0\' )';
  28.                     }
  29.                 }
  30.             }
  31.         }
  32.         print $this->query;
  33.  
  34.         /* Make query */
  35.         $result = mysql_query($this->query, $this->connection) or die(mysql_error());
  36.         if($result == true){
  37.             return true;
  38.         }
  39.         else{
  40.             $this->error['insert'] = "Error while inserting";
  41.             return false;
  42.         }
  43. }
  44.  
if you have any suggestions... please let me know

Thank you very much,


Best Regards,
Sep 20 '08 #1
Share this Question
Share on Google+
3 Replies


Atli
Expert 5K+
P: 5,058
Hi.

The only thing I can see that *might* be causing this is the math in the first variable you add:
Similar to:
Expand|Select|Wrap|Line Numbers
  1. $var = "(". 1 + 1 .")";
  2.  
Try encapsulating that, like:
Expand|Select|Wrap|Line Numbers
  1. $var = "(". (1 + 1) .")";
  2.  
Sep 20 '08 #2

bilibytes
100+
P: 128
Hi.

The only thing I can see that *might* be causing this is the math in the first variable you add:
Similar to:
Expand|Select|Wrap|Line Numbers
  1. $var = "(". 1 + 1 .")";
  2.  
Try encapsulating that, like:
Expand|Select|Wrap|Line Numbers
  1. $var = "(". (1 + 1) .")";
  2.  
thank you very much for taking your time.

i have solved this problem by putting the "('" into different variables, and then i have concatenated them with the rest, it has worked properly, although i will try what you said just for intellectual curiosity. :)

thank you again

bilibytes
Sep 21 '08 #3

pbmods
Expert 5K+
P: 5,821
'+' and '.' have the same order of precedence and left associativity, so they will be evaluated from left to right unless you use parenthesis.

E.g,:

Expand|Select|Wrap|Line Numbers
  1. 'a' . 1 + 1 . 'b' = '1b'; // ((('a' . 1) + 1) . 'b')
  2. 'a' . (1 + 1) . 'b' = 'a2b';
  3.  
http://php.net/operators
Sep 24 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.