Check if a value exist i a query - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: Check if a value exist i a query (/showthread.php?tid=21862) |
Check if a value exist i a query - El Forum - 08-23-2009 [eluser]Quaotloa[/eluser] Hi, I have been using this code in "traditional PHP". $query = SELECT groupid FROM user_group WHERE groupid NOT IN(". "SELECT groupid FROM user_group WHERE userid = $userID) $array = mysql_fetch_query($query), When i want to store values NOT IN the second query... But when i use this query inside a codeingiter model or controller, i get this error message Severity: Warning Message: mysql_fetch_array(): supplied argument is not a valid MySQL result resource How can i fix it? Can it be done by active records? Regards Fredrik Check if a value exist i a query - El Forum - 08-23-2009 [eluser]davidbehler[/eluser] Code: $result = $this->db->query($query); Check if a value exist i a query - El Forum - 08-23-2009 [eluser]Quaotloa[/eluser] I know how to use active records to create arrays, I mean the function NOT IN or IN - is there a build in function in active records to do that? Compares to querys.. Like "$query = SELECT groupid FROM user_group WHERE groupid NOT IN(”. “SELECT groupid FROM user_group WHERE userid = $userID)" I tried to do what you suggested.. My query look like this now: "$result = $this->db->query("SELECT SUM(pris), timeregid FROM timereg_utlegg WHERE timeregid IN(". "SELECT timeregid FROM timereg WHERE brukerid = '$brukerid' AND dato_strtotime_format > '$sist_dato_for_lonning')"); " I get this error " A Database Error Occurred Error Number: 1140 Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause SELECT SUM(pris), timeregid FROM timereg_utlegg WHERE timeregid IN(SELECT timeregid FROM timereg WHERE brukerid = '22' AND dato_strtotime_format > '1248991200') " How can I rewrite the query to be correct for codeigniter? It works in original PHP Check if a value exist i a query - El Forum - 08-23-2009 [eluser]jedd[/eluser] [quote author="fredrik.carlsen" date="1251052364"]I know how to use active records to create arrays, I mean the function NOT IN or IN - is there a build in function in active records to do that? [/quote] Yes there is .. and it's documented in the AR section of [url="http://ellislab.com/codeigniter/user-guide/database/active_record.html"]the user guide[/url] There was also [url="http://ellislab.com/forums/viewthread/126494/]a thread recently[/url] that talked about using the where_not_in() function in a pretty snazzy way. |