So I'm trying to create a function that generates SQL query string based on a multi-dimensional array.
Example:
function createQueryString ($ arrayToSelect, $ table, $ conditionalArray) {$ queryStr = "SELECT" .implode (",", $ arrayToSelect) "$" From "$". "WHERE"; $ QueryStr = $ queryStr.implode ("and", $ conditionartire); / * needs help * / $ queryStr;} $ ColumnsToSelect = array ('id', ' User name '); $ Table =' table '; $ status = array (' lastname '= & gt;' dow ',' zipcode '=> gt45;' '12345'); echo createQueryString ($ columnsToSelect, $ table, $ Conditions); * * The result of incorrect SQL syntax will be / / As you can see that I need help in the third line because it is currently printing
SELECT ID, from the username table where Last name and zipcode
but it should be printed
SELECT ID, last name = 'doe' and zipcode = '12345'
You are not actually inserting a multi-dimensional array of $ conditions is an associative array.
Just use a foreach loop inside your function createQueryString (). This should work some way, do not ignore it.
$ terms = count ($ conditionalArray); Foreign Currency ($ $ conditional $ Field = & gt; $ value) {$ terms--; $ QueryStr = $ field '=' $ value; If ($ word) {$ queryStr = 'AND'; }}Note: To prevent SQL injection, the values should be saved and / or cited as appropriate / needed for DB employed . Not only copy and paste; Think!
Comments
Post a Comment