PHP: Add quote() method for raw SQL access
PHP: Add quote() method for raw SQL access
GeorgeIoak
Posts: 27Questions: 6Answers: 0
I see that this was added to the Editor in the 1.3.0 release (https://editor.datatables.net/download/1.3.0) but I haven't found any details on it.
Would I just take my editor instance and add something like this:
->quote("WHERE myUDF(" . $_GET['Lname'] . ") < 3")
What I'm looking for is the ability to use my UDF in a where clause
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
Hi,
It is available in the API documentation for the PHP libraries here: http://editor.datatables.net/docs/1.4.0-beta/php/class-DataTables.Database.html#_quote
You would use it something like:
Regards,
Allan
Hi Allan:
Thanks for looking at this. I must be misunderstanding something because I'm getting an invalid JSON response when I try to use the new quote:
Fatal error</b>: Call to undefined method DataTables\Editor::quote()
The way I implemented is with this code:
I'm sorry but I've tried several times to get the code formatted properly.
Okay, the key thing to note here is that Editor does not currently support SQL functions at all. So using
damlev()
is no going to work regardless of how you construct the query.This is actually something that I've been looking at making possible in 1.4 (currently in beta). Let me get back to you tomorrow when I've had a chance to experiment a bit.
Regards,
Allan
OK, if I wasn't clear I am currently running the 1.4 beta
What you can do is to use
where()
with an anonymous function which gives you access to theQuery
instance Editor is using. That then gives you the ability to specify additional options, including using a function in the field parameter.Consider the simple case where we want to use
abs()
to get all records with an absolute value > 1 for the columnsample
:Applying that to your own
damlev
function:This requires the 1.4 libraries (for anyone else reading this!).
Regards,
Allan
This is close. I fixed a couple of minor errors but it's still throwing a SQL syntax error, near the "')" but it looks OK to me. Yesterday I was trying to navigate through the code to find where I could log the actual SQL statement being sent to MySQL but I never figured it out. That would be helpful now to find the syntax error or perhaps something will pop out at you.
On a side note, why do I continue to have trouble getting code to display properly. Sometimes it works as expected but most times I lose all line breaks no matter how I paste the code in
Frustratingly the code highlighting doesn't "refresh" when you post a comment. You would need to reload the page for the syntax highlighter to kick in. I really need to get around to looking into fixing that...
Take a look in
Database/Driver/Myql/Query.php
. In the_prepare
method there should be a commented outfile_put_contents()
which I use for debugging. You could add your own debug there, or uncomment mine and see what is going on.Allan
Thanks for pointing that location, that really helps to see what's going on. That allowed me to see we were double quoting the last name so I made a minor modification:
I figured why bother with one more step unless you can think of a reason to use
$db->quote
. Actually maybe I can already, if the last name includes a single quote my version would break.You don't quite get the entire SQL statement in the
_prepare
method. I was seeing this:which was enough for me to see the double quotes around Miller but I had to double check that :where_0 really was the 2 (I just ran some tests to see if the results looked reasonable).
Yes - you open yourself to an SQL injection as you have it above! It would be trivial for someone to do:
Lname = true';DROP ALL DATABASES;'
...!Allan
Very good point. I hadn't thought of that because lname is actually populated from an autocomplete form on the page but that doesn't stop anyone from reading the AJAX call and changing it.