php - Parametrized query unknown total params -


I am using the safemysql class for parametrized queries Generally, when creating a query, it becomes like this Is:

  $ entries = $ db-> Match all ("From the table with selection =? I and name =?", $ Age, $ name);   

Such queries where I know that the total number of parameters parsed in advance is quite straightforward, but it appears that I have been stacked on queries, where I do not Know how many parameters I use - eg One search form:

What I would like to do is the firing query parameter:

  if ($ _ POST ['namesparts']) {$ parts = explode (' ', $ _ POST [' at name '']); '$' In the form of $ ($) as '$' or 'firstname`. $ "$'; } If ($ _ POST ['age']) $ q = "and` age` = '". $ _ POST ['age']. ' '' '; $ Entries = $ dbs-> Match all ("Select from table = 1". $ Q "");   

Any suggestions?

With user-supplied information with any other SQL query, it is actually safe To handle (or instead of leaving that work, where it is related).

Yes, let's start from that goal and do not ignore it - if the query [ provided by the user] in the query If the code (and secure SQL usage) is one of the tenants violation and the following no the essential truth is now!

[safemysql] is safe because every dynamic query part [or "bit of user data"] goes into the query through the placeholder.

then resolve query text with placeholder and data array dynamically - but create separately There is no time DQL (SQL syntax) and the data is mixed. This is isolation (and a lower level guarantee) which guarantees that when this approach is not followed.

  $ data = array (); $ Q = "SELECT * FROM table WHERE 1 = 1"; If ($ _ POST ['namesparts']) {$ parts = explosion ('', $ _ POST ['' at name '']); Foreign currency ($) as part of $ part {$ q = "and (like 'name' or 'like first name')"; $ Data [] = '%' $ share '%'; // Add one for every replacement $ data [] = '%' $ share '%'; } If ($ _ POST ['age']) {$ q = "and" age "=? I"; $ Data [] = $ _POST ['age']; }}   

And now we have query text with placeholder and is the array of data to force. Yippee, we are almost there ! Now, make that array that will be passed.

  $ params = array ($ q); $ Params = array_merge ($ params, $ data); $ Entries = call_user_func_array (array ($ dbs, 'getAll'), $ params);   

And, Finish!

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -