i am using codeigniters sql selection to select users where not in a set of id's.
$this->db->select('fbuid')->where_in('fbuid', $friends);
$query = $this->db->get('users');
im am trying to add another clause where it check for and not in friends table (id column). im trying to add use
$this->db-where_not_in
but i can't seem to get it to work
heres a pseudocode sql statement
SELECT fbuid FROM users WHERE IN () BUT NOT IN the friends TABLE ID COLUMN
Try this:
$query = $this->db->select('fbuid')
->from('users')
->where_in('fbuid', $friends)
->where_not_in('fbuid', $otherFriends);
EDIT:
You can either make a subquery before and store it in array OR do this in one step:
$query = $this->db->select('fbuid')
->from('users')
->where_in('fbuid', $friends)
->where('`fbuid` NOT IN (SELECT `uid` FROM `another_table`)', NULL, FALSE);
NULL, FALSE are important.
Related
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());
is it possible to pass the variable value in mysql query in codeigniter ?
I want to select records from database that have id same as my session id.
my code is :
$this->db->select('*'); // <-- There is never any reason to write this line!
$this->db->from('products');
$this->db->where('id_admin = (SELECT id_admin FROM users WHERE id='.$this->session->userdata('user_id').')', NULL, TRUE);
can we concatenate variable values in database queries?
This code not given any error, but not give any result also.
Use join in active records
$this->db->select('p.*');
$this->db->from('products as p');
$this->db->join('users','p.id_admin=users.id');
$this->db->where('users.id',$this->session->userdata('user_id'),false);
$query = $this->db->get();
An Example Reference of how to write the JOIN Query in CI as per the Documentation of the Active Records.
$article_id = $this->input->post('article_id');
$this->db->select('*');
$this->db->from('articles');
$this->db->join('news', 'news.id = articles.id');
$this->db->where('articles.id',$article_id);
$query = $this->db->get();
The above code will produce the query as follows for execution.
SELECT * FROM articles JOIN news ON news.id = articles.id WHERE articles.id='10'
Provided the passed id is 10
In order to view the Result of the executed query you must perform the below code.
print_r($query->result());
Try like this..use joining of two tables.
<?php
$this->db->select('products.*');
$this->db->from('products');
$this->db->join('users','products.id_admin=users.id_admin');
$this->db->where('users.id',$this->session->userdata('user_id'));
$query = $this->db->get();
print_r($query->result_array());//your array of records
I would save the id of each user in a session for get later name ... in my login system. I would do that on each login.
But the problem: If i Select more columns with the sql i cant fetch a single column.
How i can fetch one column even tough i selected more columns in the sql.
I tried something like that:
$sth->fetch(PDO::FETCH_ASSOC)['password']
But it isn´t working on my code:
$sth = $X['dbh']->prepare("SELECT `password` `id` FROM `users` WHERE `uid` = :uid; ");
if (! $sth->execute(array(
':uid' => $X['param']['uid'],
))) {
//check for right name, passwort ...
}
How i can fetch one column even tough i selected more columns in the sql.
Nohow.
Either select only one column, or fetch all that you have selected. Otherwise it would make no sense to select.
And after fetching an array, you can access its members all right.
$user = $sth->fetch(PDO::FETCH_ASSOC);
$pass = $user['password'];
then later in the code you can use $user['id'] as well.
I want to delete from ‘table1’ those rows where (user_id = 5) but I should check if those posts’ (title = title1 in table2). I use Codeigniter and I get this error while trying to delete: ‘Deletes are not allowed unless they contain a "where" or "like" clause.’ Could you please help me to check what is wrong with my code below.
table1:
table2:
public function delete($title, $user_id){
$this->db->select('table1.*');
$this->db->from('table1','table2');
$this->db->where('table1.user_id', $user_id);
$this->db->where('table2.title', $title);
$this->db->join('table2','table1.post_id=table2.post_id');
$query = $this->db->get();
if ($query && $query->num_rows() > 0) {
$this->db->delete('table1.*');
$this->db->from('table1','table2');
$this->db->where('table1.user_id', $user_id);
$this->db->where('table2.title', $title);
$this->db->join('table2','table1.post_id=table2.post_id');
return true;
}
else {
return false;
}
}
Make use of subqueries.
example
#Create where clause
$this->db->select('id');
$this->db->from('table2');
$this->db->where('table2.title', $title);
$where_clause = $this->db->get_compiled_select();
#Create main query
$this->db->where('table1.user_id', $user_id);
$this->db->where("`id` NOT IN ($where_clause)", NULL, FALSE);
$this->db->delete('table1');
References
stolen from here: https://stackoverflow.com/a/16303021/1275832
about subqueries: http://www.mysqltutorial.org/mysql-subquery/
compiled select: https://github.com/NTICompass/CodeIgniter-Subqueries
After running first query you will get the set of user that you have to delete.
Run this set throw foreach loop for getting id of user and posts that you have to delete to an array.
$user_array = array();
$post_array = array();
foreach($query->result() as $query)
{
$user_array[$query->user_id] = $query->user_id;
$post_user[$query->post_id] = $query->post_id;
}
And then
this->db->where_in('user_id', $user_array)->delete('table1');
this->db->where_in('post_id', $post_array)->delete('table2');
I know that this is not the best decision. But i think that this is the most understandable.
You may use the following code to delete data from tables becasue codeigniter ignore join when you delete multiple data from multiple tables.
$sql = "DELETE t1 FROM table1 t1
JOIN table2 t2 ON t1.thing_id = t2.id
WHERE t2.otherthing_id = ?";
$this->db->query($sql, array($id));
JOINS are used to fetch data from database not used to delete data if you want to delete data from multiple table using single query then you need to use cascading in MySQl using that you can delete data also from other tables that are related to current table.
I am using Codeigntier to run the a query like this:
$query = $this->db->query("SELECT users.*, user_profiles.* FROM users, user_profiles WHERE " . "users.id = $user_id AND user_profiles.user_id = $user_id");
$row = $query->row();
I have placed an echo to get the last query $this->db->last_query(). Which shows:
SELECT users.*, user_profiles.* FROM users, user_profiles WHERE users.id = 6850 AND user_profiles.user_id = 6850
The result it returns is:
object(stdClass)[24]
public 'id' => string '6849' (length=4)
The id the result shows is 6849. I nearly fell of my chair! I ran the same query on my MySQL database and the id returned for that exact same query is 6850. 6850 is the correct ID.
I have no idea how to debug this as this is the simplest of queries. Any help appreciated.
Explicitly select your columns, and AS *something* alias them. Then check the ID's.
It is likely that the id of the table user_profiles is being shown rather than the actual user id from the users table that you expect as you are not specifying the columns.