I want to get a time difference from data using the TIMESTAMPDIFF function, but this time i want to use a pure query builder in codeigniter
$this->db->select("TIMESTAMPDIFF(DAY, (".$this->db->select('payment_date')."), (".$this->db->select('download_date').")))",FALSE);
$query = $this->db->get('transaksi');
return $query;
I've tried the code above, but it shows an error like this :
Severity: 4096 Message: Object of class CI_DB_mysqli_driver could not
be converted to string
and like this :
You have an error in your SQL syntax; check the manual that
corresponds to your MariaDB server version for the right syntax to use
near '), ())) FROM transaksi' at line 1
SELECT payment_date, download_date, TIMESTAMPDIFF(DAY, (), ())) FROM
transaksi
is there any solution to get the data?
Solution:
$this->db->select("payment_date, download_date, TIMESTAMPDIFF(DAY, payment_date, download_date)",FALSE);
$query = $this->db->get('transaksi');
return $query->result();
Sub Query not necessary there.
$this->db->select("payment_date, download_date, TIMESTAMPDIFF(DAY, payment_date, download_date)",FALSE);
$query = $this->db->get('transaksi');
return $query->result();
Related
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
By executing below query i getting this error message
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'status=1' at line 1 SELECT username,useremail FROM tbl_cart where user_id= 8AND status=1
query
$query = $this->db->query('SELECT username,useremail FROM tbl_cart where
user_id= '.$this->session->userdata('userId').'AND status=1' );
$resultdata['results'] = $query->result_array();
You need to insert an space before AND
$query = $this->db->query('SELECT username,useremail FROM tbl_cart where
user_id= '.$this->session->userdata('userId').' AND status=1' );
^ here
The better way to do it with CI is
$query = $this->db->select('username, useremail')
->where('user_id', $this->session->userdata('userID'))
->where('status',1)
//instead of from CI does get
->get('tbl_cart')->result_array();
result_array() returns the results as an array of arrays while result returns query as an array of objects
if you are using CI it is better to fully take advantage of the query builder classes
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')
I'm working at summing two different rows and their field values (forum_total_topics and forum_total_posts) from a MySQL database in ExpressionEngine using the Active Record Class provided. I have tried multiple versions of code to sum the numbers. I have attempted and failed at passing a MySQL query to the request by saying.
$SQL = "SELECT forum_id, sum(forum_total_topics) + sum(forum_total_posts)
FROM exp_forums"
ee()->db->select($SQL);
$query = ee->db->get('exp_forums');
echo $query;
to echo the total sum, and getting the following error:
Error Number: 1064
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 '' at line 2
SELECT (SELECT SUM(forum_total_topics) + SUM(forum_total_posts) FROM (exp_forums)
A messy solution
So I've tried splitting the entire request up to find a solution.
I finally got the follow piece of code working by returning multiple arrays and summing those, but it looks quite messy. How, can I return and sum the two rows from the table in one line or so?
$topics = ee()->db->select_sum('forum_total_topics')->get('exp_forums');
foreach($topics->result_array() as $topicrow) {
$totalTopics = $topicrow['forum_total_topics'];
}
$posts = ee()->db->select_sum('forum_total_posts')->get('exp_forums');
foreach($posts->result_array() as $postrow) {
$totalPosts = $postrow['forum_total_posts'];
}
$total = $totalTopics + $totalPosts;
Any suggestions would be greatly appreciated!
Tried suggestions
I attempted suggestions like so,
$SQL = "SELECT forum_id, SUM(forum_total_topics + forum_total_posts) AS total
FROM exp_forums
GROUP BY forum_id";
$query = ee()->db->select($SQL);
echo $query;
With this error instead.
A PHP Error was encountered
Severity: 4096
Message: Object of class CI_DB_mysql_driver could not be converted to string
Filename: libraries/Functions.php(679) : eval()'d code
Line Number: 98
Try it like this instead
SELECT forum_id, sum(forum_total_topics+forum_total_posts)
FROM exp_forums
GROUP BY forum_id
sqlfiddle demo
The first thing that you tried does not work due to a small syntax error I believe...
It should be:
$SQL = "forum_id, sum(forum_total_topics) + sum(forum_total_posts) FROM exp_forums"
The initial SELECT is being added in your class function so it should not appear in your $SQL:
ee()->db->select($SQL);
That's why your error starts:
SELECT (SELECT
This is also the case with the code under "Tried Suggestions".
Try this
SELECT forum_id, SUM(forum_total_topics + forum_total_posts) AS total
FROM exp_forums
GROUP BY forum_id
SELECT `id`, `name_person`, `person_content`, `datetime`
FROM (`achievers_unverified`)
WHERE ` name_person LIKE '%ved%'
OR ` person_content LIKE '%ved%' LIMIT 10
This is the sql query i am trying to use where ved is the search term.
i am gettin a 1064 error.
the codeigniter code generating it is.
$this->db->select($select)
->from($table)
->like($str[1], $query, 'both')
->or_like($str[2], $query, 'both')
->limit($offset+10, $offset);
this is the 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 'person_content LIKE '%ved%' LIMIT 10' at line 3.
$str = explode(",", $select);
where $select = id, name_person, person_content, datetime
found the solution use trim($str[0]) and trim($str[1]) the sapce was creating the problem.