Delete Mysql record using array - mysql

I am trying to delete multiple record(s) by using codeigniter
$this->db->delete() and $this->db->where()
I want to delete record(s) with an array like
$id =array(
0=>'13', //13 is the id
1=>'4', //4 is the id
2=>'2' //2 is the id
);
The array was generated by the users so it will be dynamic.
I just want to know if codeigniter can take the array as a option in delete method.
According to this. http://codeigniter.com/user_guide/database/active_record.html
array won't work in the following method.
$this->db->delete()
$this->db->where('id', $id); //I don't think I can do this.
I could use foreach to loop but it seems to me there are better ways. I was wondering if anyone could help me about it. Thanks a lot.

Not really familiar with codeigniters active record but I believe the stmt you would like is:
$sql = "DELETE FROM tbl WHERE id IN (".implode(',',$idsToDelete.");";
$this->db->query($sql);
This might work better with active record:
$this->db->where('IN ('.implode(',',$idsToDelete).')', NULL, FALSE);
$this->db->delete();

The method where really accept an array as parameter?
I think your source code should look:
$this->db->delete()
$this->db->where($id['i_index']);

This post is dated, but I figured still deserves the best/right answer:
$this->where_in('id', array(13, 4, 2))->delete('db_table_name');

Related

How to retrieve data from json column using codeigniter?

I’m a beginner with codeigniter. I’m trying to retrieve data from json column of my sql db
The db structure is like this
DB NAME : order
——————————————————------------------------------------------
Id | description
——————————————————————————————————---------------------------
1 | {“pc”: [{ “brand”: “name”}], “mouse”: [“LL”, “DC”]}
————————————————————————————————————--------------------------
For example, I want to retrieve all instances that has mouse = dc
$data = $this->db->select($select)->from(‘order’)
->where(“mouse”, “DC”) ->get()->result();
Well, I've never worked like this before, but with the help of the user ascsoftw answer, I've found this section on the MySQL documentation that might help, I think this is even better for you case.
Then, your query may look something like this (sorry if I'm mistaken, like I said, never worked like this before):
SELECT Id FROM order where description->>"$[1][1]";
From what I understand that would retrieve all the Id's of the mouse (array index 1) with DC's description (array index 1 as well).
You may keep reading the docs of MySQL if that didn't help.
By the way,is worth mention that, you have several ways to do queries in CodeIgniter, if the CI query builders does not adapt to what you want to do, you can always do as the following example:
$query = "SELECT * FROM table_name";
$result = $this->db->query($query);
if($result->num_rows() != 0)
{
foreach($result->result() as $row)
{
return something
}
}
else
{
return false
}
Let me know if that helped you so I can edit with the correct answer for anyone running into the same problem.

get sql query from collection model

I know that we can get sql query from magento model collection using this,
getSelect();
But I can get query from only the model collections, not worked in others or May be I dont know how to use it.
Here I want to know what query is running behind this,
$productModel = Mage::getModel('catalog/product')->getCollection();
$attr = $productModel->getResource()->getAttribute("color");
if ($attr->usesSource()) {
echo $color_label = $attr->getSource()->getOptionText("28");
}
If I use this,
echo $productModel->getSelect(); exit;
I'm just get the one part the query only, like,
SELECT `e`.* FROM `catalog_product_entity` AS `e`
Update:
This is my full code,
<?php
require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app();
$productModel = Mage::getModel('catalog/product')->getCollection();
$attr = $productModel->getResource()->getAttribute("color");
if ($attr->usesSource()) {
$color_label = $attr->getSource()->getOptionText("28");
}
$productModel->printlogquery(true);exit;
Please help me guys,
Your order condition is not visible in the query. The reason your order isn't showing is because the orders are added to the query during the load() method.
See Varien_Data_Collection_Db::load()
Try calling load(true) to see the complete SQL containing the order by clause.
$productModel->load(true);
$productModel->printLogQuery(true);
Hope it helps.
If you want to see what is the exact query, then you can get this by using:
$productModel->printlogquery(true);exit;
Use this code after you have loaded your model's object and applied all conditions.
I hope this will help you.
Magento collecting data with lot of internal queries - models and lot of checks, and may be more than 1 table. So it is not possible to get the query like what I'm looking for.

codeigniter query db using select statement

I am new to CodeIgniter, please help me to write a select statement in CodeIgniter equivalent of
$username='JOHN';
$query=SELECT id FROM table1 WHERE username=$username
Just the format, the rest I'll figure out. Thanks.
One liner to get the id..
return $this->db->select("id")->where("username", $username)->get("table1")->row();
OR
Since id is unique and you're retrieving only one row, add a limit 1 like,
return $this->db->select("id")->where("username", $username)->get("table1", 1)->row();
Hope this helps :)
$this-db->select('id');
$this->db->where('username', $username);
return $this->db->get('table1');
But, as fancyPants said, this is easily findable in all kinds of tutorials around the web. Not a good question.
refer this codeigniter link
visit: https://www.codeigniter.com/user_guide/database/query_builder.html
That would translate to Active Record:
$username='JOHN';
/* allows you to specify selecting a column */
$this->db->select('id');
/* does the FROM, and WHERE */
$query = $this->db->get_where('table1', array('username' => $username));
And to start off, please read the documentation.

Rails - update directly in SQL

Delving into the documentation and the api, I seem to be missing how to update one field in multiple rows at once.
Something like
Table.select(:field).update("update to this").where(id: 4,5,6)
would be nice.
Does something like this exist? It would be much better than having to store everything in an array, set it to a value, and calling save every time.
You can use the update_all method, for example:
Table.update_all("field = 'update to this'", ["id in (?)", ids])

DBIx::Class update only one row

I am using DBIx::Class and I would like to only update one row in my table. Currently this is how I do it:
my $session = my_app->model("DB::Session")->find(1);
$session->update({done_yn=>'y',end_time=>\'NOW()'});
It works, but the problem is that when it does find to find the row, it does this whole query:
SELECT me.id, me.project_id, me.user_id, me.start_time, me.end_time, me.notes, me.done_yn FROM sessions me WHERE ( me.id = ? ): '8'
Which seems a bit much when all I want to do is update a row. Is there anyway to update a row without having to pull the whole row out of the database first? Something like this is what I am looking for:
my_app->model("DB::Session")->update({done_yn=>'y',end_time=>\'NOW()'},{id=>$id});
Where $id is the WHERE id=? part of the query. Does anyone know how to do this? Thanks!
You can run update on a restricted resultset which only matches this single row:
my_app->model("DB::Session")->search_rs({ id=> 1 })->update({done_yn=>'y',end_time=>\'NOW()'});
I suggest you use a DateTime->now object instead of literal SQL for updating the end_time column because it uses the apps servers date and time instead of the database servers and makes your schema more compatible with different RDBMSes.
Do you have a check if the row was found to prevent an error in case it wasn't?
You might want to use update_or_create instead.
You could use the "columns" attribute:
my $session = my_app->model("DB::Session")->find(1, {columns => "id"});