trying to execute a Doctrine query that is invariably not happening:
The code I have:
$q = Doctrine_Query::Create()->select('l.lid')->from('lessons l')->where('l.topic =?','Title of topic')
$result = $q->fetchOne() ;
The funny thing is, this returns the wrong column, that is not l.lid but l.someOtherColumn
So not sure where I am goofing up, your comments and critics are much appreciated.
Thanks!
Try to use Create('table_name t ') instead of ->from() ....
Related
Can anybody please help me achieving the result and solve my problem. I'm working in codeigniter where i want to fetch records from a mysql database using OR statement. I'm using custom model extension by Avenirer. I want to know how to use or operator in where function of this MY_MODEL:
https://github.com/avenirer/CodeIgniter-MY_Model/blob/version-3.0/core/MY_Model.php
You can do it in two ways.
You use or_where
$this->db->where('column1', 'like');
$this->db->or_where('column2', 'like');
or you can write both in a where
$this->db->where("(column1='like' OR column2='like')");
Hope it helps you out.
I can't give you an exact answer for your code, because I don't know exactly where the error is. If you could write the line to it, that would be useful.
I figured out the answer by studying the code in MY_MODEL file.
The right way to write where with OR clause was to use their special syntax.
As per the code in the file MY_MODEL, it states the supported arguments for where function, which are:
where($field_or_array = NULL, $operator_or_value = NULL, $value = NULL, $with_or = FALSE, $with_not = FALSE, $custom_string = FALSE)
Setting the 4th argument true will change the final syntax to OR statement.
Right statement in my case will be:
$this->Table_model->where('field1','value1')->where('field2','value2',null,true)->get_all();
This statement will be processed to following SQL statement.
SELECT * FROM Table WHERE 'field1'='value1' or 'field2'='value2'
Thanks all for their cooperation.
$datas=Elan::orderBy('created_at','desc')->where('status', 1)->take(4)->toSql();
dd($datas);
I wrote query in "laravel 5.2". I displayed it as sql query with function toSql()
and the result is:
"select * from `els` where `status` = ? order by `created_at` desc limit 4"
you can see that there is ? mark. this is why my query doesn't work. why does it return me ? instead of one. I also tried it like this '1' same result.
You may want to read about bindings if you want to understand what does ? mean.
https://laravel.com/docs/5.3/database#running-queries
If you want to debug and watch queries, you can install this debugbar: https://github.com/barryvdh/laravel-debugbar
? is used for Parameter binding which prevent against SQL injection.
If you want to see the raw SQL query which is executed with parameters values, try the below code:
DB::enableQueryLog();
$datas=Elan::orderBy('created_at','desc')->where('status', 1)->take(4)->get();
dd(DB::getQueryLog());
Hope this helps!
This portion of a Doctrine query is only returning results for "validated = 1". How do I modify this query so it will also include results for validated = 3? This is my first "IN".
$query
->andWhere($query->expr()->in('m.validated', ':validated'))
->setParameter('validated', '1,3');
Try this code instead,
$query
->andWhere('m.validated IN (:validated)')
->setParameter('validated', array('1','2'));
Or with the same code giving values in an array.
Hope this helps.
Cheers!
I'm using CodeIgniter, I want to using short table name for my SQL Query like that:
select pr.name from product pr where pr.id=12;
by using db class, I supposed to do:
$this->db->select('pr.name')->from('product pr')->where('pr.id', 12)->get();
This works perfect on CI 2.1.3. Don't forget to use result().
Example works for me:
function test(){
$this->load->database();
$sql = $this->db->select('pr.order_id')->from('items_table pr')->where('pr.order_id', 2)->get();
foreach($sql->result() as $item){
echo $item->order_id;
}
}
You can do it in this form:
$this->db->select('pr.name');
$this->db->from('product as pr');
$this->db->where('pr.id', 4);
return $this->db->get()->row_array();
with row_array() you will get one row, with result_array() you will get result of array.
There are so many solutions.. I prefer HMVC, but for demo purpose to explain "how it works", solutions inside controller (terrible,sucks!!!!)
Answer for using shorts table as alias, pls read rtfm or use simple query and generating results
For join methods,you can use too. Happy coding
I cannot understand what is wrong with following query:
var tmp = dbset.AsEnumerable<mytable>().Where(
c=>c.Barcode.Contains("11") && c.Description.Contains("EW") );
return tmp;
Should this be same as:
select * from mytable
where Barcode like '%11%' and Description like '%EW%';
If I run this in sql server i get four rows as it should be, but not when I run the linq query
i get 0 rows.
Can please someone help me. It is rather a simple query and it is giving me such a headache. Thank you very much
You forget fetch data, do this:
var tmp = dbset.AsEnumerable<mytable>().Where(
c=>c.Barcode.Contains("11") && c.Description.Contains("EW") );
return tmp.ToList();
Also do not call AsEnumerable soon, use it as below:
var tmp = ctx.mytable.Where(
c=>c.Barcode.Contains("11") && c.Description.Contains("EW") );
return tmp.ToList();
dbset.AsEnumerable<mytable>()...
Don't do that!
You are causing all your data to get pulled from the database before checking the Where condition.
Other than that, it's not really clear why your query isn't working. Your syntax is correct. I'm guessing the data doesn't actually look like you think it does. Either that, or you're expecting like %EW% to match something in a case-insensitive manner and since the Where clause is being evaluated by LINQ to Objects you're not getting that behavior.
Run a query with only one condition? " c.Barcode.Contains("11") ".
That code should run fine.