4

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

2 Answers 2

12

Try separate the query

 $this->db->query("SET @weekVideoCount := (SELECT COUNT(*) FROM videos v)");
 $query= $this->db->query("SELECT @weekVideoCount");
Sign up to request clarification or add additional context in comments.

1 Comment

FYI this works too SELECT @rn:=@rn+1 AS rank FROM (SELECT (SELECT @rn:=0) )
2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.