bazianm Posted July 4, 2014 Share Posted July 4, 2014 Hi, I would like to store sql query strings in a database. A query might look this like: SELECT * from ListTable where id = '$id' The idea would be to retrieve the string and run it as a SQL string. $id would be defined in the procedure so the idea is that when I send the SQL String through, $id would be replaced with its value and everything would work. Under normal circumstances, the code would look like this: $id = "somevalue"; $lcSQL = "SELECT * from ListTable where id = '$id'"; I would pass the SQL select off to mySQL and all is right with the world. I just want to store the string in a database, retreieve it and use it. Thanks for your help in advance... Quote Link to comment https://forums.phpfreaks.com/topic/289440-storing-sql-selects-in-a-table-and-running-them-in-php/ Share on other sites More sharing options...
ginerjm Posted July 4, 2014 Share Posted July 4, 2014 So - just do it? What is your problem with doing this, albeit it doesn't make sense to me. Quote Link to comment https://forums.phpfreaks.com/topic/289440-storing-sql-selects-in-a-table-and-running-them-in-php/#findComment-1483855 Share on other sites More sharing options...
CroNiX Posted July 4, 2014 Share Posted July 4, 2014 So, you want to query the db in order to retrieve another query, which then you'll presumably execute? That's 2 queries when it should just be one. Doesn't sound very efficient. Why not just execute the query you want to run in the first place? Quote Link to comment https://forums.phpfreaks.com/topic/289440-storing-sql-selects-in-a-table-and-running-them-in-php/#findComment-1483858 Share on other sites More sharing options...
davidannis Posted July 4, 2014 Share Posted July 4, 2014 $id = "somevalue"; $lcSQL = "SELECT * from ListTable where id = '$id'"; $result=mysqli_query($link, $query); if ($result){ $row=mysqli_fetch_assoc($result); $result=mysqli_query($link, $row['stored_query']); //do stuff here }else{ //handle error here echo 'No stored query for that id'; } Quote Link to comment https://forums.phpfreaks.com/topic/289440-storing-sql-selects-in-a-table-and-running-them-in-php/#findComment-1483859 Share on other sites More sharing options...
Jacques1 Posted July 4, 2014 Share Posted July 4, 2014 Stuffing queries into the database makes absolutely no sense whatsoever. It makes even less sense when I look at the actual query. All that does is retrieve one particular record from one table, which means the only relevant parameters are the ID the possibly the table. Anyway, if you want proper help, you need to tell us the actual problem, not what you think is the solution. Quote Link to comment https://forums.phpfreaks.com/topic/289440-storing-sql-selects-in-a-table-and-running-them-in-php/#findComment-1483861 Share on other sites More sharing options...
bazianm Posted July 4, 2014 Author Share Posted July 4, 2014 So, you want to query the db in order to retrieve another query, which then you'll presumably execute? That's 2 queries when it should just be one. Doesn't sound very efficient. Why not just execute the query you want to run in the first place? Well, here's the idea. I am creating a shell controller and view (I am using CodeIgniter). I can write one view and one controller that can control many different master tables for data entry. The differences between the various tables can be stored in a table. So, if I want to run the "customers" page, I can query the database to get all the information I need to run the customers page. Among the things that are different from table to table is the SQL select I will have to run the retrieve data for the page. So, one of the columns in the "pages" table is the SQL Select I will run in order to retrieve a specific customer record. By data driving the specific page information, I can save a lot of time in development. So, I will have a field called cRowSQL and it could have, as a string, the following SQL Select: SELECT customer.id, customer.name, customer.address, customer.city, customer.state, customer.zip, SUM(invoices.totalamount) as sales FROM customer LEFT OUTER JOIN invoice ON invoice.idcustomer = customer.id WHERE customer.id = '$id' I want to retrieve the SQL select and replace in the $id with the real value so I can get the row I want. If I were writing the code directly, I would encase the string in a double quote and it would evaluate the variable immediately. I want the same functionality except I am not storing it as a liternal string into SQL code variable. I am trying to figure out how to do that... Does this help? Quote Link to comment https://forums.phpfreaks.com/topic/289440-storing-sql-selects-in-a-table-and-running-them-in-php/#findComment-1483864 Share on other sites More sharing options...
bazianm Posted July 4, 2014 Author Share Posted July 4, 2014 Stuffing queries into the database makes absolutely no sense whatsoever. It makes even less sense when I look at the actual query. All that does is retrieve one particular record from one table, which means the only relevant parameters are the ID the possibly the table. Anyway, if you want proper help, you need to tell us the actual problem, not what you think is the solution. The simple query was just an example. The point of putting the queries into a database saves me from duplicating code. In effect, I am using data driven application development. I could have pages that join multiple tables and have multiple filters in the where clause... Quote Link to comment https://forums.phpfreaks.com/topic/289440-storing-sql-selects-in-a-table-and-running-them-in-php/#findComment-1483866 Share on other sites More sharing options...
davidannis Posted July 4, 2014 Share Posted July 4, 2014 You can use str_replace like this: <?php $var='$id'; $id='123'; $select='SELECT * FROM mytable WHERE id=\'$id\''; $answer= str_replace($var, $id, $select, $count); echo $answer; ?> There must be more elegant solutions with preg_match to make sure you get variables that are whole words ($apple not treated as $a.'pple') and I'm guessing with $$ Quote Link to comment https://forums.phpfreaks.com/topic/289440-storing-sql-selects-in-a-table-and-running-them-in-php/#findComment-1483876 Share on other sites More sharing options...
davidannis Posted July 4, 2014 Share Posted July 4, 2014 I'm thinking use a delimiter to pick out the variables. You can then do something like this: <?php $var='id'; $id='123'; $select='SELECT * FROM mytable WHERE id=\'~id~\''; $answer= str_replace(('~'.$var.'~'), $$var ,$select, $count); echo $answer; ?> You can pass str_replace an array of values to replace multiple variables in one pass. Quote Link to comment https://forums.phpfreaks.com/topic/289440-storing-sql-selects-in-a-table-and-running-them-in-php/#findComment-1483877 Share on other sites More sharing options...
gizmola Posted July 5, 2014 Share Posted July 5, 2014 I know you think that this idea is actually simpler, but it's really not. If you had a decent base model class you could create all the child models in no time at all and be able to implement your admin/crud without sticking sql strings into a database. Quote Link to comment https://forums.phpfreaks.com/topic/289440-storing-sql-selects-in-a-table-and-running-them-in-php/#findComment-1483891 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.