codeigniter multiple OR query - php

I want to use following query in Codeigniter:
SELECT `users` . * , `users_profile` . * FROM (`users`) LEFT JOIN `users_profile`
ON `users`.`id` = `users_profile`.`users_id`
WHERE (`loginid` = '$username' OR `email` = '$username' )
AND `password` = '$password' AND `users`.`status` = 'A' LIMIT 1
It is working fine, but when I write this query in Codeigniter format:
$this->db->where('loginid',"$username");
$this->db->or_where('email', "$username");
$this->db->where('password',MD5($password));
$this->db->where('users.status','A');
$this->db->limit(1);
It will return always true what is right syntax?

May be your query should be like this in codeIgniter.......for reference you can see this link
Reference
$this->db->select('*');
$this->db->from('users');
$this->db->join('users_profile', 'users_profile.id = users.id', 'left');
$this->db->where("(loginid = '$username' OR email = '$username') AND password = 'MD5($password) AND status = 'A')'");

You should use the custom string method of where() as this is not possible directly with only ActiveRecord. See this link from the documentation and scroll to the 4th option under the where() function.
Example:
Custom string:
You can write your own clauses manually:
$where = "name='Joe' AND status='boss' OR status='active'";
$this->db->where($where);
$this->db->where() accepts an optional third parameter. If you set it to FALSE, CodeIgniter will not try to protect your field or table names with backticks.
$this->db->where('MATCH (field) AGAINST ("value")', NULL, FALSE);

Related

Give an alias in a join statement without changing the select statement (*) in codeigniter

I'm working with queries in espressionengine, and I'm coding them with the codeigniter syntax. Here's my query:
$sql = ee()->db->select('*');
$sql = ee()->db->from('sometable');
$sql = ee()->db->where('id', $hidden_id);
$sql = ee()->db->get();
I'd like to add a join in order to get another value like that:
$sql = ee()->db->join('another_table', 'sometable.another_table_id, another_table.id);
Now the problem is that in the join statement I add another column with the same name (id).
Instead of changing the select statement (*) I'd like to add an alias in the join statement something like:
$sql = ee()->db->join('another_table', 'sometable.another_table_id, another_table.id as another_table_id);
Is it doable?
Hope this will help you :
And your query should be like this :
Use this if both primary and foreign key has same column name :
$this->db->select('*');
$this->db->from('sometable stable');
$this->db->join('(SELECT id
FROM another_table ) as a_table' ,'id');
$query = $this->db->get();
print_r($query->result());
if not please use this :
Make sure in join here a_table.id = stable.id , primary and foreign key match here
$this->db->select('*,a_table.id as credit_id');
$this->db->from('sometable stable');
$this->db->join('another_table a_table','a_table.id = stable.id');
$query = $this->db->get();
print_r($query->result());
You can also get result from this query with the help of $this->db->query();
$sql = 'your query here';
$query = $this->db->query();
print_r($query->result());

php select users not found in another table when the values in that table are blank

I have a query that is supposed to look through the rota table and select the idstaff an then find all users that are not currently found in that result. As long as idstaff is not null this query works fine however there are cases where idstaff will be NULL. In this case the query fails. Is there any way I can make it work in both instances?
$query = sprintf("SELECT user.*
FROM user
WHERE user.iduser NOT IN (SELECT idstaff FROM rota WHERE idrota=%s)",
$this->db->GetSQLValueString($idrota, "int"));
$result = $this->db->query($this->db->link, $query) or die($this->db->error($this->db->link));
Taken straight from my comment, your query string should be like this:
$query = sprintf("SELECT user.*
FROM user
WHERE user.iduser NOT IN (SELECT idstaff FROM rota WHERE idrota=%s AND idstaff <> NULL)",
$this->db->GetSQLValueString($idrota, "int"));
You should add NULL because sql skips these records by default.
$query = sprintf("SELECT user.*
FROM user
WHERE user.iduser IS NULL OR NOT IN (SELECT idstaff FROM rota WHERE idrota=%s)",
$this->db->GetSQLValueString($idrota, "int"));
$result = $this->db->query($this->db->link, $query) or die($this->db->error($this->db->link));

SELECT query failing after updating it

I modified the following query and am now getting this error.
Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result,
boolean given
I have read into what this error means and I know it is my query that is wrong. This worked perfectly before I changed the query. It was this before...
$query = mysqli_query($con, "SELECT * FROM users WHERE `group` = 3");
I changed it to...
$query = mysqli_query($con, "SELECT * FROM users WHERE `group` = 3, 4 ,5");
$array = array();
while ($row = mysqli_fetch_assoc($query)) {
What am I doing wrong in the new query?
your query has a mistake.
Try this:
$query = mysqli_query($con, "SELECT * FROM users WHERE `group` in (3, 4 ,5)");
The in serves the same function as an OR
Your query is indeed wrong, you cannot check a value like that.
If you want to check against multiple values, you can make use of the IN keyword.
"SELECT * FROM users WHERE `group` IN (3, 4 ,5)"
If you want to check of either one of the choice use IN instead:
$query = mysqli_query($con, "SELECT * FROM users WHERE `group` IN (3, 4 ,5)");
For comma separated value use IN
SELECT * FROM users WHERE `group` IN(3,4,5);
you may try this following also,may be you will get your result.
SELECT * FROM users WHERE group = 3 or group = 4 or group = 5;
Thanks.

How to write the given query in CodeIgniter

I want to write the given query in codeigniter , how can I do so?
SELECT * FROM `user` WHERE `id` IN (select `provider_id` from `services`);
I gone through codeigniter's where_in() but i'm getting wrong result , here what I have done,
$this->db->select('*');
$this->db->from(USER);
$this->db->where_in('id', 'select `provider_id` from `services`');
$query = $this->db->get();
return $query->num_rows();
it is returning 0, while the query above, when run directly in phpmyadmin, returns 1 record.
You can use this simple way $this->db->query('your query');
Please check this post, hope it will help you Using Mysql WHERE IN clause in codeigniter
You can pass direct queries in where class by passing second parameter as NULL and third parameter as FALSE
<?php
$where = 'id IN (select `provider_id` from `services`)';
$this->db->select('*');
$this->db->from(USER);
$this->db->where($where, NULL, FALSE);
$query = $this->db->get();
return $query->num_rows();
Use $this->db->query();
$query=$this->db->query("SELECT * FROM `user` WHERE `id` IN (select `provider_id` from `services`)");
$num=$query->num_rows();
$result= $query->result();

Laravel Order By certain value

I'd like for one of my relationships to return ordering by a certain value as follows:
return $this->hasMany('Photo', 'owner_id')->where('active', '=', '1')->where('visible', '=', '1')->orderByRaw("(`id` = ?) DESC", array($showphoto));
But the above code returns this :
select * from `photos` where `photos`.`owner_id` = '4' and `active` = '1' and `visible` = '1' order by (`id` = '') DESC
How do I get the $showphoto variable to be in the order by?
It won't show you that it was injected since you are not currently triggering the query.
When you will chain the get() method to it, it will "inject" the $showphoto and execute the query.
I am not aware of a way you can see the full query including the values of the parameters before actually executing the query.

Categories