pravonline Posted May 24, 2006 Share Posted May 24, 2006 Dear All,I would like to how to concatenate the values from an array to form a complete sql query, say for example The array looks like this :Arraytest[1] = "SELECT"Arraytest[2] = "*"Arraytest[3] = "FROM"Arraytest[4] = "TABLE1"Arraytest[5] = "WHERE"Arraytest[6] = "NAME"Arraytest[7] = "="Arraytest[8] = "BUSH"And from this i need to form a SQL qury like this ""SELECT * FROM TABLE1 WHERE NAME = 'BUSH'" and need to pass this into a Variable "$test", number of subscript of this array depends upon the length of query, can any one guide me how to do this? Since i'm a mainframe professional and new to this technology!!Thank you,Antony Quote Link to comment https://forums.phpfreaks.com/topic/10325-concatenation-of-variables-from-an-array-to-forma-a-sql-query/ Share on other sites More sharing options...
hvle Posted May 24, 2006 Share Posted May 24, 2006 This script below only work with SELECT, meaning it won't work if the query is update, insert ....you can do a little hack first, it is to insert a \' around values (like BUSH) in this example.[code] for ($i = 0; $i < count($Arraytest); $i ++) { if ($Arraytest[$i] == '=') { $Arraytest[$i+1] = "'". $Arraytest[$i+1] . "'"; $i ++; } }[/code]Above block will find any occurent of = and wrap the single quote around the next one.[code] $sql = implode(' ', $Arraytest);[/code]above code simply concat the array by space.I understand you want a code that worked with all query. However it is depend on what kind of query to concatenate the sql. Quote Link to comment https://forums.phpfreaks.com/topic/10325-concatenation-of-variables-from-an-array-to-forma-a-sql-query/#findComment-38499 Share on other sites More sharing options...
Honoré Posted May 24, 2006 Share Posted May 24, 2006 Probably impossible to do without additional information.You need to know which array elements are SQL reserved words, table or column names, data values.Non numeric values should appear between quotes, database - table - column names should be between back quotes, ... Quote Link to comment https://forums.phpfreaks.com/topic/10325-concatenation-of-variables-from-an-array-to-forma-a-sql-query/#findComment-38500 Share on other sites More sharing options...
pravonline Posted May 24, 2006 Author Share Posted May 24, 2006 Dear Mr.hvle, [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /] Thanks for your valuable code and suggestion, I acheived the required result with your sample code, Thank you so much once again Mr. Hlve. Dear Mr.Honoré,Thanks for your valuable suggestionRegards,Antony Quote Link to comment https://forums.phpfreaks.com/topic/10325-concatenation-of-variables-from-an-array-to-forma-a-sql-query/#findComment-38615 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.