Jump to content

Concatenation of variables from an array to forma a sql query


pravonline

Recommended Posts

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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, ...
Link to comment
Share on other sites

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 suggestion

Regards,
Antony
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.