0

I need some help solving a problem with MySQL, is it possible to pass an array to a function and then run a match against the array values?

I have this query

public function getMenu($cookieId)
{
    $this->db->select('*');
    $this->db->from('categoryTable');
    $this->db->join('userMenuTable', 'categoryTable.categoryId = userMenuTable.categoryId', 'left');
    $this->db->where('userMenuTable.cookieId', $cookieId);
    
    $query = $this->db->get();
    return $query->result_array();
}

Using the $query array that is returned is possible to query the database and get all the values from a table that do not match the array values?

2
  • Can you post the contents of $query? I have not used CI myself, but understanding how the array is returned would help in answering how you can call the query. Commented Feb 15, 2010 at 15:05
  • "Is it possible?" Yes. You have not provided enough details for us to provide a working solution. When you say "that do not match the array values", do you mean some subset of values from result_array()? Or do you mean that $cookieId might contain an array? Are we talking about adjusting this query or creating a new query? Is it desirable extend this query by filtering on a subquery (or appending another JOIN)? By the way, you can pass categoryId as a long string and CodeIgniter will create USING categoryId as the JOINing condition. Commented Apr 11 at 3:11

2 Answers 2

1

Use this condition in your query:

$this->db->where_not_in('fieldname', $array_of_values);

You won't be able to directly use the array returned in your example, as it comes from a SELECT * and thus it contains all fields of the table. You have to build an array with ONLY the values of the field you want to filter you next query on.

Sign up to request clarification or add additional context in comments.

Comments

0

What columns do you have in that array ? Theoretically you could do a

select from `new table` where `field` NOT IN (Select `field` from `old_table`)

to do this in just one query, or pass your array the NOT IN condition

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.