Named variables in sql query |
HI,
I have this query which runs perfect on plain sql (phpmyadmin etc) : Code: SELECT from post data, I have the following code in my model : Code: Public function get_results () { Code: $this->db->query($sql, [ getting replaced by the actual data coming from the post data (which I can see if I echo the relevant named variable) . I tried just to be sure to hard code the data in the code to check like this in the model : Code: $this->db->query($sql, [ Maybe I need to escape the named variables somehow but I have tried escaping them with single quotes or ` ` but no change . I tried to use active record also but I cant make a proper query like the one I already have here. George
Try this, not tested but fixes some errors in your code see the quotes and semi-colons at the end.
PHP Code: $sql = 'SELECT What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
(06-26-2021, 01:51 AM)InsiteFX Wrote: Try this, not tested but fixes some errors in your code see the quotes and semi-colons at the end. Thanks but the profiler still returns the tart: :end: in the query instead of actual values : Code: Error Number: 1064 Never mind I found a way to do it with active record actually and some php logic in between . I wanted also same CASE WHEN conditions and finally it was much easier with active record Thanks Anyway !!
Try this one, no time to test right now busy.
You need to pass in the start and end strings for the dates to search. PHP Code: $sql = 'SELECT What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
(06-26-2021, 05:41 AM)InsiteFX Wrote: Try this one, no time to test right now busy. Just tested this also , not working either here is my full code in the model : Code: $sql = 'SELECT I've tried your suggestions as well as numerous other ones , the blinded values are not getting replaced no matter what . Also the named bindings documentation seems to be referencing a very old version of codeigniter . In the query builder class I can only find the documentation about bindings which is very different . Anyway I can't be the only one here with a similar problem , nobody else in here ever wanted to create a dynamic query using raw sql ? Some other similar queries I've managed to convert to query builder but this one uses MONTHNAME and COUNT to return some values which I haven't figure out yet if I can do in a single query as in raw sql.
If you try to insert variable in a string you need to use double quotes. Single quotes will just ignore the dollar sign and take it as literal text.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/ |
Welcome Guest, Not a member yet? Register Sign In |