473,399 Members | 3,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

PHP concatenated string part desaperas

bilibytes
128 100+
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
3 1512
Atli
5,058 Expert 4TB
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
128 100+
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
5,821 Expert 4TB
'+' 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

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

Similar topics

7
by: M Wells | last post by:
Hi All, I have what seems to me to be a difficult query request for a database I've inherited. I have a table that has a varchar(2000) column that is used to store system and user messages...
6
by: Rudolf Bargholz | last post by:
Hi , I have the following tables ------------- PAX: Id Order_Id Name Position
2
by: B Love | last post by:
Hello Group, I have 2 text fields that I would like to concatenate for use in a table. One field is an ordinary text box. The other is a simple combo box which I use to select one of about ten...
9
by: Dr. StrangeLove | last post by:
Greetings, Let say we want to split column 'list' in table lists into separate rows using the comma as the delimiter. Table lists id list 1 aa,bbb,c 2 e,f,gggg,hh 3 ii,kk 4 m
4
by: Kurt | last post by:
I'm using the fConcatChild function posted at http://www.mvps.org/access/modules/mdl0004.htm to return a field from the Many table of a 1:M relationship into a concatenated string. The function...
1
by: Jeff Silverman | last post by:
I have a PHP program that almost works. I'm running it from the command line and simulating a form using a GET method. That part is working, but I get spurious records with all of the fields...
7
by: eric.goforth | last post by:
Hello, I'm working with a classic asp page that calls another classic asp page. The html in my calling page looks like: <form method="post"...
4
by: Jazzer | last post by:
I want to 'flatten' two tables into one by combining the '1-n' values from the 'child' records into a single concatenated string within the parent by using queries. I.E. create a single NVarChar...
1
by: Brad Isaacs | last post by:
Using ASP.NET 2.0 with SQL Server 2000 Inside my dropdown list box I am using an SQL DataSource to select the following data SELECT RTRIM(c.Name_First) + ' ' + RTRIM(c.Name_Last) AS Contact,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.