However nutty this may be, I would like to make a way in Wappler to search every piece of data inside every table inside my database.
I start off with a custom query
SELECT * FROM information_schema.`COLUMNS` C WHERE TABLE_SCHEMA = 'mydatabasename';
I add schema for
TABLE_NAME Type: Text
COLUMN_NAME Type: Text
I then add a repeat to that and it outputs perfectly, with all table names and all column names in my entire database
Inside the repeat step I would now like to add the following secondary custom query
SELECT :P2 FROM :P1;
:P1 has been set to {{TABLE_NAME}}
:P2 has been set to {{COLUMN_NAME}}
Now I run this and get an error, the reason for the error is my syntax of the query
SELECT 'my_column' FROM 'my_table';
If this were without the single quote marks around the column and table name it would be valid. like this
SELECT my_column FROM my_table;
Question is, is there a way to force :P1 and :P2 to output as literal values and not as strings like this.
I tried changing the Schema returned from query 1 to Number instead of Text but that seemed to make no difference.
I saw that, and kind of figured the same however all those posts do not seem to able to get around the issue of getting the table name and column name dynamically, while so far this is actually almost working, got a dynamic table name, got a dynamic column name, but those single quotation marks ' are breaking it
I used something similar a week ago, and it works fine.
Not sure about the single quotes though. I don’t think column and table names even need that.
Maybe try without that.
There was a similar discussion here. I think it can be definitely done using a stored procedure, but @sid’s method looks interesting (though I haven’t been able to get it to work yet). @sid offered alternative suggestions in the other thread too.
Thanks @sid and @TomD, I got this working, so Sid you quite correct, I have to use the direct bindings rather than the parameters, because the parameters add the single quote characters and turn everything into strings, the second thing is that MySQL does not like the column name of index sitting inside the wappler_migrations_lock table, so I also have to tell my first query to ignore that table in its search, or I need to use the backtick character arount all the column names to tell the query the word index is not a command but a column name.
Here is the final working script for anyone that may need something like this
Just by the way the reason I am trying to do this is to land up with a similar functionality that phpMyAdmin has where I can search for all instances of something over my entire database.
Thanks, I did take note of that on my side, im not even keeping this live, im making it using it to fix an issue and then removing it.
I know this would be a huge security hole indeed.
I don't think you would want to create queries using this method for any public-facing site, but it's a powerful feature. You can add precautions, like only accepting certain table names or rejecting certain strings or characters in queries etc.
I didn't realise the technique was possible - and am surprised that it is. When I asked @George about this, when the custom query feature was added, he said:
you can’t have tables or column names as parameters.
This is how PDO prepared statements work with their parameters.
It is also saving you from a lot of sql injection attacks.
I wonder if @sid's 'secret' technique was intended to be possible. If not, hopefully it won't be changed to make it not work. I think it should be an option, as it would be if you were writing code by hand, in PHP for example.
I agree Tom, it’s not something I would use in any form of production as such, but there are instances where it’s easier doing this than a sql dump and using a text editor.
Nice to see it’s actually achievable though.
@JonL, would using the built in server connect security providers be enough if it were intended for usage just by the super administrator, so only one person would have credentials to use it. But would that security method be enough to stop attacks on a script like this.
Was just going to say what a nice discussion here about “if I leave my house key under the doormat, would it be safe enough?”
No, you don’t want to leave the key under the doormat, you want to lock the door and keep the key in your pocket.