473,598 Members | 3,252 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PHP concatenated string part desaperas

bilibytes
128 New Member
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,as df3) 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,as df3) 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 1534
Atli
5,058 Recognized Expert Expert
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 New Member
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 Recognized Expert Expert
'+' 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
2815
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 from an on-line ordering system. For some reason (I have no idea why), when the original database was being designed no thought was given to putting these messages in
6
8510
by: Rudolf Bargholz | last post by:
Hi , I have the following tables ------------- PAX: Id Order_Id Name Position
2
2377
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 items. The ordinary text field is: NewLongDescription (This is a memo field that contains descriptions of dolls sold in an e-comm site)
9
14707
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
2969
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 puts everything in a comma separated format. In my case, it returns a list of authors: Borman, W., Oppler, S., White, L., I'd like to change the function so the last record in the string is preceded by an ampersand instead of a comma, as in:
1
1653
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 concatenated into a single field. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta content="text/html; charset=windows-1252" http-equiv="content-type">
7
1916
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" action="/includes/mypage2.asp?subtype=MyType&amp;ecd=1&amp;eid=97702"> ....Do some stuff
4
2591
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 field to hold all of the values that are contained in the child entries for that parent. E.G. Parent 1 - Child 1 Value "Big"; Child 2 Value "Small" Parent 2 - Child 1 Value "Red"; Child 2 Value "White"; Child 3 Value "Green" To Become...
1
2368
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, c.phone As PriPortPhone FROM mppaContacts c Dropdown list box works well .....
0
7987
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7899
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8050
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8264
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6718
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5850
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3939
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1504
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1250
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.