6

Mysql codeigniter query is not working properly. Suppose if mysql table looks like this:

 user_id|user_name
       1|john
       2|alex
       3|sam

Here user_name is unique

The following query should return false if user_name=john and user_id=1 and true if say user_name=john and user_id=2.

$this->db->get_where('user', array('user_name' => $name,'user_id !=' => $userid));

But it returns true in the case user_name=john and user_id=1.

Can anyone suggest me an alternative way of querying not equal to.

print($this->db->last_query()) gives:

SELECT * FROM (user) WHERE user_name = 'john' AND user_id != '1'

6
  • Any chance you have any additional users with first name john? That would return true as well. Commented Feb 1, 2013 at 5:03
  • 1
    try this and check what query is been executed print "SQL Query: ".$this->db->last_query(); Commented Feb 1, 2013 at 5:10
  • ok this is how it looks like SELECT * FROM (user) WHERE user_name = 'john' AND user_id != '1'. The 1 was the no of rows returned. Commented Feb 1, 2013 at 5:19
  • that was correct it will return true only you are checking whether user_id!= or not obviously user_id is not equal because 1 != 2 so it will return true Commented Feb 1, 2013 at 5:42
  • This will help you [same question][1] [1]: stackoverflow.com/questions/5226261/… Commented Dec 24, 2013 at 21:49

5 Answers 5

3

Why dont you use simple $this->db->query('your query');

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

2 Comments

i would suggest you to use procedures as they are pre parsed and complied and use $this->db->query('call yourprocedure('your parms')');
I am amazed that this is the accepted answer. It makes no attempt to resolve the specific question asked and there is absolutely no intent to educate the OP or future researchers. Very low value on Stack Overflow.
3

Simply try this, Add the desired condition in the where function.

$this -> db -> where('invitee_phone !=', $user_phone);

1 Comment

This entirely unexplained answer bears no resemblance to the question asked and fails to respect the actual challenge that the OP is facing. Very low quality.
1

You can go follwoing way too. It work for me

$total = 5;
$CI = get_instance();
$CI->load->database();
$CI->db->order_by('id','asc');
$topusers = $CI->db->get_where('users',array('user_type != 1  && `status` =' => 1),$total,0);
echo $CI ->db ->last_query();
die;

and if still not work for you can go with @rohit suggest: $this->db->query('your query');

1 Comment

Is && valid sql syntax for your db driver? What is the point of using order_by? Why is there an arbitrary limit of 5 imposed? Why did you invent new/unmentioned table columns in your query? last_query() doesn't produce a result set, it displays the rendered query. This answer isn't much of an answer. Not only is it doing bad/unnecessary things, there isn't a shred of explanation.
1

Type 1:

Using ->where("column_name !=",$columnname) is fine for one column.

But if you want to check multi columns, you have to form an array inside where clause.

Like this

$whereArray = array(
                "employee_name" => $name,
                "employee_id !=" => $id,
        );

$this->db->select('*')->from('employee')->where($whereArray);

Type 2:

We can just write exactly what we want inside where.

Like

$thi->db->where(("employee_id =1 AND employee name != 'Gopi') OR designation_name='leader@gopis clan'");

Type 2 is good for working with combining queries, i mean paranthesis "()"

2 Comments

The OP doesn't actually need to select any data into a result set; only a count is needed. You are not fully embracing CI's query builder methods -- this makes your suggestion less portable. Assuming variables would be involved in the where values, this technique will be unstable/insecure because there will be no automatic escaping.
anyway thanks for the != mark inside the string of array, that helps me alot to customize the Where statements... @AravidhGopi
0

you can follow this code:

$query = $this->db->select('*')->from('employee')->where('user_name', $name)->where('user_id !=', $userid)->get();
$last_query = $this->db->last_query();
$result = $query->result_array();

if you pass $name = 'john' and $userid = '1' then it return empty array.

1 Comment

Not only do I believe the where logic is incorrect on this answer, the OP doesn't actually need to generate a result set of data from the query. Only the count is required. This is doing more than it needs to and there are more concise methods that can be used.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.