Please tell me how to pass OR condition in CodeIgniter query. My query like:
$where=array('status' => 'pending','linux_added_on >='=>$from_date,'linux_added_on <='=>$to_date);
And I want to add:
$where=array('status' => 'pending OR Approve','linux_added_on >='=>$from_date,'linux_added_on <='=>$to_date);
Please help me.
Here is an example. Change query to your needs:
$sql = "SELECT * FROM some_table WHERE id = ? AND status = ? AND author = ?";
$this->db->query($sql, array(3, 'live', 'Rick'));
The question marks in the query are automatically replaced with the values in the array in the second parameter of the query function.
Related
can I convert this query
"SELECT * FROM (SELECT * FROM blog_post ORDER BY data DESC) blog_post GROUP BY blog_id LIMIT 2"
into a Yii2 active record query?
Thx
Ms
Yes, you can do this.
Yii2 gave us a wonderful library support.
You can form custom sql query and pass this query in findBySql() like:
$sql = "Some query/nested query";
$result = ModelClass::findBySql($sql);
Visit Yii official documentation.
BlogPost::find()
->orderBy('data DESC')
->groupBy('blog_id')
->limit(2)
->all();
I suppose you can do in this way :
Ⅰ:create a subQuery where you select from.
$blogPostQuery = BlogPostModel::find()->orderBy(['data' => SORT_DESC]);
Ⅱ:Get activeRecord results. The from params is an exist Query.
$models = (new yii\db\Query)
->from(['blog_post ' => $blogPostQuery])
->groupBy(['blog_id'])
->limit(2)
->all();
ps:
see yii\db\query->from()-detail in Yii Api;
(public $this from ( $tables )) $tables can be either a string (e.g. 'user') or an array (e.g. ['user', 'profile']) specifying one or several table names··· or a sub-query or DB expression
Have a try!I hope it`s useful for you:))
I am trying to get URL parameter in SQL, but nothing happens.
Here is my URL:
http://localhost/webshop/imagegallery.php?categori=necklace
Here is my SQL query:
$sql = 'SELECT count(productid) FROM products where productcategori=".$_GET["categori"]"';
What am I doing wrong?
Have a look at this query, too:
$sql = 'select * from products join ids on products.productid=ids.productid join photos on photos.photosid=ids.photoid where products.productcategori='".$_GET["kategori"]."' && ids.photonumber=1 ORDER BY products.productid DESC $limit';
First of all, your quotation marks seem to be the problem. Try changing your query line to this:
$sql = "SELECT count(productid) FROM products where productcategori='".$_GET["categori"]."'";
Further, you should never insert variables into a SQL query like this. Never.
The reason is that like this, your system is vulnerable for SQL injections.
Instead consider using PDO. This SO question has a nice answer on how to do it correctly.
Using that answer, this is some example code regarding the last part of your question. Note that I replaced all variables in your query string by PDO placeholders.
<?php
$pdo = new PDO('mysql:dbname=mydatabase;host=127.0.0.1;charset=utf8', 'username', 'password');
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM products JOIN ids ON products.productid=ids.productid JOIN photos ON photos.photosid=ids.photoid WHERE products.productcategori=:categori && ids.photonumber=1 ORDER BY products.productid DESC LIMIT :limit_min , :limit_max";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':categori', $_GET['categori']);
$stmt->bindParam(':limit_min', ($pagenum - 1) * $page_rows, PDO::PARAM_INT);
$stmt->bindParam(':limit_max', $page_rows, PDO::PARAM_INT);
$stmt->execute();
foreach($stmt as $row) {
// do something with $row
}
?>
In SQL, I might do something like this
"WHERE DATEPART(month, my_date_field) = 8"
to grab the rows where month in my_date_field = 8.
I am unsure how this syntax translates into Yii2.
I have
$query = Fees::find();
$fees = $query->all();
How do I use the WHERE clause on the date field within that $query ?
The query in yii2 allows text in where clause as below
$fees = Fees::find()
->where("DATEPART(month, my_date_field) = 8")
->all();
You may chain other 'sql functions' like order, group by , limit etc like this as well.
Basicly i have two tables photos and users. I wanna join tables and Update colums image_max and image_min. I get error unknown colum username. In which way i can join two tabels and get data from both. My sintax is:
$this->db->select('*');
$this->db->from('photos');
$this->db->join('users', 'photos.id = users.id');
$this->db->where('username',$username);
$this->db->update('photos',$data);
And I get error
Unknown column username in where clause
UPDATE `photos` SET `image_max` = '', `image_min` = '' WHERE `username` = 'wwww'
apparently you need a letter on the table should say "users.username", check that.
Greetings.
$this->db->select('*');
$this->db->from('photos');
$this->db->join('users', 'photos.id = users.id');
$this->db->where('users.username',$username);
$this->db->update('photos',$data);
You don't need to use "select and from" before upload fields, just update in this way
$data = array('image_max'=> 4, 'image_min' => 1);
$this->db->join('users', 'photos.id = users.id');
$this->db->where('username',$username);
$this->db->update('photos',$data);
I have the following function in one of my CI models:
function update_uu_feature($feature, $lang, $data)
{
$feature = str_replace('_', '-', $feature);
$this->db->where('navigation', $feature);
$this->db->where('language', $lang);
$this->db->join('all_video_names', 'all_video_names.video_id = all_uu_features.video_id', 'inner');
return $this->db->update('all_uu_features', $data);
}
But the “join” line gets discarded. In the profiler, it outputs:
UPDATE `all_uu_features` SET `page_title` = 'Laser', `title` = 'Test', `keywords` = 'Test', `description` = 'Test', `feature_text` = '<p>Test text</p>', `learn_more_about` = 'Test Text 2', `overview` = 'test', `copy` = 'test' WHERE `navigation` = 'laser-interface' AND `language` = 'en'
Can someone help me with the syntax? Thanks. Most of the data is in the all_uu_features table, except for the video name, which is in the all_video_names table. The two are joined on the video_id column in each table. In other words, all of the columns in the above statement are in all_uu_features except for video name. Hope that's clear.
The SQL statement above shows the data is being returned in the post and being processed by the function, it's just not accepting the join for some reason.
It looks to me very much like CI's Active Record class doesn't support using joins in its update() operation. Certainly if you have a look at the code (in DB_active_rec.php), the update() method doesn't even look at any joins that have been set up
From update():
$sql = $this->_update($this->_protect_identifiers($table, TRUE, NULL, FALSE), $this->ar_set, $this->ar_where, $this->ar_orderby, $this->ar_limit);