[PHP] Help with SQL Query String
Ave, Simple problem.. Following is a statement I¹m inserting in my PHP Script... But it doesn¹t work because I¹m not able to figure out how to put the variables with the trim() function in the SQL insert statement: $sql = INSERT INTO tblname (USERID,FULLNAME,SSN,STARTDATE) VALUES (trim($row[USERID]),trim($row[FULLNAME]),trim($row[SSNO]),trim($row[STARTDAT E])); I can of course predefine a unique variables for each of the $row[] variables with the trim() function and then use those variables in my INSERT statement... But I¹m sure there is a way this statement can work, I¹m just not able to figure it out and would love if anyway made a suggestion. Thanks, Rahul S. Johari Coordinator, Internet Administration Informed Marketing Services Inc. 251 River Street Troy, NY 12180 Tel: (518) 266-0909 x154 Fax: (518) 266-0909 Email: [EMAIL PROTECTED] http://www.informed-sources.com
RE: [PHP] Help with SQL Query String
[snip] $sql = INSERT INTO tblname (USERID,FULLNAME,SSN,STARTDATE) VALUES (trim($row[USERID]),trim($row[FULLNAME]),trim($row[SSNO]),trim($row[STAR TDAT E])); [/snip] Time to quote and concatenate and make pretty... $sql = INSERT INTO tblname (USERID,FULLNAME,SSN,STARTDATE) ; $sql .= VALUES ( ; $sql .= ' . trim($row['USERID']) . ', ; $sql .= ' . trim($row['FULLNAME']) . ', ; $sql .= ' . trim($row['SSNO']) . ', ; $sql .= ' . trim($row['STARTDATE']) . ' ; $sql .= ) ; This will make things easier to maintain as well. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Help with SQL Query String
You should not just give him the code but rather tell him why. 1. trim() is a php function. MySQL does not know what to do with it. You need to place it 'outside' of the sql. You can also do something like this: $userid = trim($row['USERID']); Then use $userid in your sql. 2. Items in arrays must be in quotes. jzf Jay Blanchard wrote: [snip] $sql = INSERT INTO tblname (USERID,FULLNAME,SSN,STARTDATE) VALUES (trim($row[USERID]),trim($row[FULLNAME]),trim($row[SSNO]),trim($row[STAR TDAT E])); [/snip] Time to quote and concatenate and make pretty... $sql = INSERT INTO tblname (USERID,FULLNAME,SSN,STARTDATE) ; $sql .= VALUES ( ; $sql .= ' . trim($row['USERID']) . ', ; $sql .= ' . trim($row['FULLNAME']) . ', ; $sql .= ' . trim($row['SSNO']) . ', ; $sql .= ' . trim($row['STARTDATE']) . ' ; $sql .= ) ; This will make things easier to maintain as well. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Help with SQL Query String
[snip] You should not just give him the code but rather tell him why. 1. trim() is a php function. MySQL does not know what to do with it. You need to place it 'outside' of the sql. You can also do something like this: $userid = trim($row['USERID']); Then use $userid in your sql. 2. Items in arrays must be in quotes. [/snip] Ya'll bitch when I make them RTFM, ya'll bitch when I do codewhat's a guy to do? j/k Actually http://dev.mysql.com/doc/mysql/en/string-functions.html shows that MySQL also has a trim function which could be applied thusly; $sql = INSERT INTO tblname (USERID,FULLNAME,SSN,STARTDATE) ; $sql .= VALUES ( ; $sql .= TRIM(' . $row['USERID'] . '), ; $sql .= TRIM(' . $row['FULLNAME'] . '), ; $sql .= TRIM(' . $row['SSNO'] . '), ; $sql .= TRIM(' . $row['STARTDATE'] . ') ; $sql .= ) ; -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Help with SQL Query String
Jay Blanchard wrote: Ya'll bitch when I make them RTFM, ya'll bitch when I do codewhat's a guy to do? j/k Don't get married? ;) -- John C. Nichel ÜberGeek KegWorks.com 716.856.9675 [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Help with SQL Query String
Ave, Thanks a lot folks. I did actually mention that doing something like $userid = trim($row['USERID']); And then using those variables in my SQL statement would work... The only reason I chose to make this post however and not do that was because I wanted to know if it can be done the other way. I do understand though that using the PHP trim() function in an SQL statemnt won't work. However the new snip about mySQL having it's own TRIM() function is also pretty cool. Thanks again all, Rahul S. Johari Coordinator, Internet Administration Informed Marketing Services Inc. 251 River Street Troy, NY 12180 Tel: (518) 266-0909 x154 Fax: (518) 266-0909 Email: [EMAIL PROTECTED] http://www.informed-sources.com On 3/31/05 10:30 AM, Jay Blanchard [EMAIL PROTECTED] wrote: [snip] You should not just give him the code but rather tell him why. 1. trim() is a php function. MySQL does not know what to do with it. You need to place it 'outside' of the sql. You can also do something like this: $userid = trim($row['USERID']); Then use $userid in your sql. 2. Items in arrays must be in quotes. [/snip] Ya'll bitch when I make them RTFM, ya'll bitch when I do codewhat's a guy to do? j/k Actually http://dev.mysql.com/doc/mysql/en/string-functions.html shows that MySQL also has a trim function which could be applied thusly; $sql = INSERT INTO tblname (USERID,FULLNAME,SSN,STARTDATE) ; $sql .= VALUES ( ; $sql .= TRIM(' . $row['USERID'] . '), ; $sql .= TRIM(' . $row['FULLNAME'] . '), ; $sql .= TRIM(' . $row['SSNO'] . '), ; $sql .= TRIM(' . $row['STARTDATE'] . ') ; $sql .= ) ; -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Help with SQL Query String
well...i would have told him to go pound sand...the php manual is great and so is the MySQL manual. People are just lazy. [/snip] Ya'll bitch when I make them RTFM, ya'll bitch when I do codewhat's a guy to do? j/k Actually http://dev.mysql.com/doc/mysql/en/string-functions.html shows that MySQL also has a trim function which could be applied thusly; $sql = INSERT INTO tblname (USERID,FULLNAME,SSN,STARTDATE) ; $sql .= VALUES ( ; $sql .= TRIM(' . $row['USERID'] . '), ; $sql .= TRIM(' . $row['FULLNAME'] . '), ; $sql .= TRIM(' . $row['SSNO'] . '), ; $sql .= TRIM(' . $row['STARTDATE'] . ') ; $sql .= ) ; -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Help with SQL Query String
On 3/31/05 12:45 PM, Joseph Connolly [EMAIL PROTECTED] wrote: well...i would have told him to go pound sand...the php manual is great and so is the MySQL manual. People are just lazy. Ave, Pound sand .. Interesting. And yes, both the manuals are great, and people are extremely lazy and purposely idiotic, nonsensical and absurdly inconclusive of the inadequacies of their lifeless brains. Have an awesome day. PS: Jay.. The code works great... Thanks a ton again! Rahul S. Johari Coordinator, Internet Administration Informed Marketing Services Inc. 251 River Street Troy, NY 12180 Tel: (518) 266-0909 x154 Fax: (518) 266-0909 Email: [EMAIL PROTECTED] http://www.informed-sources.com [/snip] Ya'll bitch when I make them RTFM, ya'll bitch when I do codewhat's a guy to do? j/k Actually http://dev.mysql.com/doc/mysql/en/string-functions.html shows that MySQL also has a trim function which could be applied thusly; $sql = INSERT INTO tblname (USERID,FULLNAME,SSN,STARTDATE) ; $sql .= VALUES ( ; $sql .= TRIM(' . $row['USERID'] . '), ; $sql .= TRIM(' . $row['FULLNAME'] . '), ; $sql .= TRIM(' . $row['SSNO'] . '), ; $sql .= TRIM(' . $row['STARTDATE'] . ') ; $sql .= ) ; -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php