Write This Query in Codeigniter - php

I need a little assistance. I need this query
SELECT *, STR_TO_DATE( end_date, "%d/%m/%Y" ) AS DATE
FROM `resume_experience`
WHERE `resume_id` =1
ORDER BY DATE DESC
to be written in codeigniter active record format. This is what I wrote.
$result = $this->db->select('*,STR_TO_DATE(end_date,%d/%m/%Y) AS DATE')
->from('resume_experience')
->order_by('DATE', "desc")
->where($where)
->get()
->result_array();
return $result;
It is giving me the following error.
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM (resume_experience) WHERE resume_id = '1' ORDER BY DATE desc' at line 2
SELECT *, STR_TO_DATE(end_date, `%d/%m/%Y)` AS DATE FROM (`resume_experience`) WHERE `resume_id` = '1' ORDER BY `DATE` desc
Any help would highly be appreciated.
Thanks and Waiting,
Ahmad

You need to pass second parameter as FALSE in select() so it will not quote the bacticks for STR_TO_DATE(end_date,%d/%m/%Y) AS DATE
$this->db->select("*,STR_TO_DATE(end_date,'%d/%m/%Y') AS DATE",FALSE)
See active record

STR_TO_DATE() requires quotes around the format string:
$this->db->select('*,STR_TO_DATE(end_date,"%d/%m/%Y") AS DATE')

Related

Change date format when adding where condition to the query

Database:
| date_paid (int) |
1535558400
1539532800
I am trying to query the following but i get SQL syntax error for the date part.
$id = $this->input->post('id');
$date = $this->input->post('date');
$this->db->where('id',$id);
$this->db->where('DATE_FORMAT(date_paid, "%Y-%m-%d")',$date);
$query=$this->db->get('payments');
$result=$query->row();
Is there any way to change the date format of the column date_paid so that i can actually compare it to the $this->input->post('date') data?
Error log:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''2018-10-30'' at line 4
I am trying to modify an existing web application that is why as much as possible i do not want to change the data type of the column date_paid
thanks in advance
Please, use like this
DATE_FORMAT(FROM_UNIXTIME(`date_paid`), '%Y-%m-%d') AS 'date_changed'
You can refer here
You need to use
DATE_FORMAT(FROM_UNIXTIME(date_paid), '%Y-%m-%d') AS 'date_formatted'
so something like:
$id = $this->input->post('id');
$date = $this->input->post('date');
$this->db->select("DATE_FORMAT(FROM_UNIXTIME(`date_paid`), '%Y-%m-%d') AS date_formatted");
$this->db->from('payments');
$this->db->where("DATE_FORMAT(FROM_UNIXTIME(`date_paid`), '%Y-%m-%d') = " . $date, '', false);
$result = $q->row();
Use the syntax below
select convert(varchar(4), year(ColumnDate))+'/'+convert(varchar(4), month(ColumnDate))+'/'+convert(varchar(4), day(ColumnDate)) from YourTable

order by str_to_date query return error while using php

I have created a query it was working perfectly on phpmyadmin
SELECT *,STR_TO_DATE(horoaccess_validity_enddate , '%d-%m-%y') as horo FROM userlogin WHERE type='user' ORDER BY horo DESC
but when I pass this query on PHP return error as query was empty
my PHP code is
$selectorders=sprintf("SELECT *,STR_TO_DATE(horoaccess_validity_enddate , '%d-%m-%y') as hor FROM userlogin WHERE type='user' ORDER BY horo DESC");
$myorderquery = mysql_query($selectorders) or die(mysql_error());
$row_rsselect = mysql_fetch_assoc($myorderquery);
You are using sprintf for no reason. Since your date format use '%' characters, you need to double them if you want to use this function anyway :
$selectorders=sprintf("SELECT *,STR_TO_DATE(horoaccess_validity_enddate , '%%d-%%m-%%y') as hor FROM userlogin WHERE type='user' ORDER BY horo DESC");
Just use a string declaration and it will work.

Converting Sql query to CodeIgniter Active Records

I wanted to query only those items that have a highest likes.
This is the sql query I want to convert into CodeIgnitor's Active Records:
SELECT *, SUM(like) as totalLikes
FROM tbl_like
GROUP BY uploadID
ORDER BY totalLikes DESC
LIMIT 2
CodeIgniter:
public function get_cheezyPic(){
$this->db->select('uploadID, SUM(like) as totalLikes');
$this->db->from('tbl_like');
$this->db->group_by('uploadID');
$this->db->order_by('totalLikes DESC');
$this->db->limit(2);
$query= $this->db->get();
return $query->result_array();}
But when I try to run this code, I've got this error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'like) as totalLikes FROM (tbl_like) GROUP BY uploadID ORDER BY totalLikes ' at line 1
SELECT `uploadID`, SUM(like) as totalLikes FROM (`tbl_like`) GROUP BY `uploadID` ORDER BY `totalLikes` desc LIMIT 2
What's wrong with this code?
Thanks for the help.
This'll work for you. Please note that you were using reserved keyword so it should be enclosed with backtic ``.
public function get_cheezyPic(){
$this->db->select('uploadID, SUM(`like`) as totalLikes',false);
$this->db->from('tbl_like');
$this->db->group_by('uploadID');
$this->db->order_by('totalLikes DESC');
$this->db->limit(2);
$query= $this->db->get();
return $query->result_array();
}

WHERE date BETWEEN not working

I am trying to count logins by date range by counting how many times the auto integer (id) appears between a start and end date.
I get the start date and end date from a form in a previous page (y-m-d).
$start_date=$_POST['start_date']; /*in this case its "2014-10-10"*/
$end_date=$_POST['end_date']; /*in this case its "2014-10-20"*/
$sql = <<<SQL
SELECT id, COUNT(*) as login_count FROM `usage`
GROUP BY id
WHERE date
BETWEEN $start_date AND $end_date
SQL;
However I keep getting the following syntax error
"There was an error running the query [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE date BETWEEN 2014-10-10 AND 2014-10-20' at line 3]"
What am I doing wrong?
try it
$sql = <<<SQL
SELECT id, COUNT(*) as login_count FROM `usage`
WHERE date
BETWEEN '$start_date' AND '$end_date'
GROUP BY id
SQL;
your $sql should be :
$sql = <<<SQL
SELECT id, COUNT(*) as login_count FROM `usage`
WHERE date
BETWEEN $start_date AND $end_date
GROUP BY id
SQL;
as GROUP BY CLAUSE is AFTER WHERE syntactically
You need to put ''s around date literals in sql like so:
$response = mysql_query("SELECT * FROM `db`.`$sql_table` WHERE (date BETWEEN '$end_date' AND '$start_date') ORDER by id ASC ")or die(mysql_error());
Firstly you should quote your dates '$start_date' AND '$end_date' and you should definitely make sure those values are escaped to avoid SQL injection.
You have mistakes in your SQL. Try this:
SELECT id, COUNT(*) as login_count FROM `usage`
WHERE (date BETWEEN $start_date AND $end_date)
GROUP BY id;

MySQL Where Clause error

i have the following statement
$result = mysql_query("SELECT * from rests ORDER BY name asc WHERE flag = '1' LIMIT 0 , 20");
am getting the following error
Invalid query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE flag = '1' LIMIT 0 , 20' at line 1
am not sure where am going wrong :(
$result = mysql_query("SELECT * from rests WHERE flag = '1' ORDER BY name asc LIMIT 0 , 20");
You cannot have the ORDER BY clause before WHERE clause.
Refer to the MySQL select syntax for the correct order of various clauses.
Try this:
SELECT * from rests WHERE flag = '1' ORDER BY name asc LIMIT 0 , 20
Also Ordering in ascending is default, you may drop the asc.
The ORDER BY clause must be after the WHERE clause. That's all it is.
ORDER BY comes after the WHERE and LIMIT at the end.
WHERE goes before ORDER BY.

Categories