Welcome Guest, Not a member yet? Register   Sign In
Named variables in sql query
#1

HI,
I have this query which runs perfect on plain sql (phpmyadmin etc) :
Code:
SELECT
MONTHNAME (itd.DateOfshippment) AS Month
,COUNT(itm.Name) AS Operations
FROM ImportsMaster itm
INNER JOIN ImportsDetails itd on itm.ID  = itd.IDMSTR
WHERE itd.Market IS NOT NULL
AND itm.Trader LIKE "%TEXT%"
AND itd.DateOfshippment between :start and :end
GROUP BY MONTH(itd.DateOfshippment)
Now I'm trying to do the same in ci (3) but I need to pass start and end variables

from post data, I have the following code in my model :

Code:
Public function get_results () {
$sql = "SELECT
MONTHNAME (itd.DateOfshippment) AS Month
,COUNT(itm.Name) AS Operations
FROM ImportsMaster itm
INNER JOIN ImportsDetails itd on itm.ID  = itd.IDMSTR
WHERE itd.Market IS NOT NULL
AND itm.Trader LIKE "%TEXT%"
AND itd.DateOfshippment between :start: and :end:
GROUP BY MONTH(itd.DateOfshippment)"
Code:
$this->db->query($sql, [
            'start' => $this->input->post('start'),
            'end' => $this->input->post('end')
        ]);
}
According to documentation I need to enclose the named variables in colons but they're not
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, [
            'start' => '2021-01-01',
            'end' => '2021-03-30'        ]);
But it also doesn't get replaced so there must be something else I'm missing here  but I can't figure out what.
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
Reply


Messages In This Thread
Named variables in sql query - by stepgr - 06-26-2021, 01:03 AM
RE: Named variables in sql query - by InsiteFX - 06-26-2021, 01:51 AM
RE: Named variables in sql query - by stepgr - 06-26-2021, 02:17 AM
RE: Named variables in sql query - by InsiteFX - 06-26-2021, 05:41 AM
RE: Named variables in sql query - by stepgr - 06-29-2021, 06:42 AM
RE: Named variables in sql query - by includebeer - 06-29-2021, 05:41 PM
RE: Named variables in sql query - by php_rocs - 06-30-2021, 06:17 AM



Theme © iAndrew 2016 - Forum software by © MyBB