Welcome Guest, Not a member yet? Register   Sign In
Check if a value exist i a query
#1

[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
#2

[eluser]davidbehler[/eluser]
Code:
$result = $this->db->query($query);
$array = $result->result_array();
#3

[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
#4

[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.




Theme © iAndrew 2016 - Forum software by © MyBB