Hey there! By dynamic query string, I do not mean prepared statements. I have a bunch of input forms and don’t know in which one information might be entered. Let’s say I have
And I don’t know what variables might be set (in order to have “INSERT INTO table (/*random vars */) VALUES (/*random vars */)”).
So I tried
list($var1, $var2, $var3) = $new_arr;
$str = "INSERT INTO table (/*random vars */) VALUES (";
for ($i = 0;i < $count($new_arr); $i++) {
if (!empty($new_arr[$i]) {
$str .= "'" . $new_arr[$i] . "',";
}
}
$str .= "')";
However, although this might work, I can’t figure out how to bind the parameters to the initial part of the query. I would consider using sprintf but I still do not know how many strings to expect so it’s pretty much useless here. Does anyone have an idea?
You don’t understand: I don’t know if those fields actually hold any variable as they are not compulsory in that segment of the application. So first, I need a loop to check which ones are set and add them to the end of the query like I did in the OP, then somehow also add their corresponding column names in the center of the query.
I quite understand actually. As I am specifically interested in creating dynamic queries, and can do it for any PHP database extension blindfolded.
What you need actually is to create an array with data, where keys are the same as column names in the table and values are values. The rest will be done automatically.
And yes - in any case your query should be using prepared statements.
`
That’s what I have now (and what I had in my mind while opening the thread). At this point, the only means of progress is to manually use if statements to check which ones are set and append them to the second part. However, if I do that, then there is no point using the loop I used earlier on and that would create a lot of markup.
First of all, it is not an SQL query, but a PHP code to generate one.
Second, a PHP code should never look like this, because it will generate an incorrect SQL.
A correct one would be
INSERT INTO table (announcements, testimony, book) VALUES (?, ?, ?);
Yours also seems correct although each variable in a valid SQL should be wrapped in quotes. Also I think empty is safer. All the same, if mine encounters any bugs(which I doubt it would) I’ll do it your way.
Then write a query where every variable is used twice
$sql = "SELECT * pro_files WHERE name=?s
(AND ?s is NULL OR year = ?s)
(AND ?a is NULL OR type = ?a)
(AND ?a is NULL OR room = ?a)
(AND ?s is NULL OR day = ?s)
(AND ?s is NULL OR time = ?s)";
And finally run the query:
$data = $db->getAll($sql,$name,$year,$year,$room,$room,$type,$type,$day,$day,$time,$time);
It will return the search result based on non-null values provided.
@elizine First of all, what’s a DBAL??
2) Where did you get the $db/getval variables from?
3) Why are those values doubled when I’m checking if they themselves have values?
4) Lastly, what does the s in WHERE name=?s mean? String?
I like both the library and the clever method of selecting things you are using, but it’s a bit irrelevant tot he question, as the OP is not using SafeMysql at the moment and his problem is not SELECT but INSERT query with unknown number of parameters.