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.
Related
the code below is executed but it brings a wrong records(all records in my table) , it's like he doesn't take on consideration the clause where
$result = $this->Posts->query('SELECT * FROM POSTS WHERE id=1');
I know that I can do it easily with find() but for some reasons I want to write the sql statement and to have the right results
Thanks for helping me.
query() method does not take any parameter. you can use it like
$data= $this->Posts->query()
->where(['id'=>1])
->execute()
->fetchAll();
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.
When working on Laravel, we do query like this:
$user = DB::table('users')->where('name', 'John')->first();
How can I view the generated sql query? This is something very important for debugging during the development.
Thank you.
According to this answer, you should be able to use this to get the last executed query :
$queries = DB::getQueryLog(); // gets a log of all executed queries
$last_query = end($queries); // gets the last one
You can also add this snippet:
Event::listen('illuminate.query', function($sql)
{
var_dump($sql);
});
It will output all queries being executed in your request.
Not a direct answer as other people have answered this but take a look at this composer package, it is very helpful and displays all of your queries and more.
https://github.com/barryvdh/laravel-debugbar
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"});
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');