Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setting mysql user-defined variable with query-builder

I have an issue with setting mysql user-defined variables with codeigniter. How am I able to use SET with CI query builder?

SET @weekVideoCount := (SELECT COUNT(*) FROM videos v );

SELECT @weekVideoCount;

When I execute this Query on Sqlyog the result will be successfully shown but if I call this Query in the model like this

function getWeeklyUserData(){
        $query= $this->db->query("SET @weekVideoCount := (SELECT COUNT(*) FROM videos v);
                                SELECT @weekVideoCount;
                                ");
        return $query->result();
    }

the error has generated

enter image description here

like image 477
Query Master Avatar asked Dec 10 '22 01:12

Query Master


2 Answers

Try separate the query

 $this->db->query("SET @weekVideoCount := (SELECT COUNT(*) FROM videos v)");
 $query= $this->db->query("SELECT @weekVideoCount");
like image 196
safarov Avatar answered Jan 01 '23 17:01

safarov


you should know $this->db->query() execute only one sql statement just like mysql_query.

like image 24
IT Advanture Avatar answered Jan 01 '23 16:01

IT Advanture