How can I update TIMESTAMP with Codeigniter and MySQL? - mysql

How can I update TIMESTAMP with Codeigniter and MySQL? I want to update with INTERVAL 1 MINUTE
I've tried the next code:
$data = array('is_active' => $state,
'timestamp_demo' => "DATE_ADD(NOW(), INTERVAL 1 MINUTE)");
$this->db->where('demo_session_id', 'web');
$this->db->update('demo_session', $data);
But not work. How can I do?

Try your query like this
$this->db->query("UPDATE demo_session
SET
is_active = 1,
timestamp_demo = DATE_ADD(NOW(), INTERVAL 1 MINUTE)
WHERE demo_session_id = 'web'");

You can do this with Active Records, which you're using:
$this->db->where('demo_session_id', 'web');
$this->db->set('is_active', $state);
$this->db->set('timestamp_demo', 'DATE_ADD(NOW(), INTERVAL 1 MINUTE)', FALSE);
$this->db->update('demo_session');
The third parameter in $this->db->set() prevents CodeIgniter from escaping the data so it'll use the MySQL function.
It'll end up something like this:
UPDATE 'demo_session' SET 'is_active' = $state, 'timestamp_demo' = DATE_ADD(NOW(), INTERVAL 1 MINUTE) WHERE 'demo_session_id' = 'web'

Related

How to check does user is not active more than 1 minute

Hello I tried to update a row in table if somebody is offline more than 1 minute, and set LoggedIn then as 0, but my SQL query doesn't work, what I do wrong?
"UPDATE acc SET LoggedIn='0' WHERE LastTimeActive<(NOW(), INTERVAL 1 MINUTE)";
You are missing date_sub():
UPDATE acc
SET LoggedIn = '0'
WHERE LastTimeActive < date_sub(NOW(), INTERVAL 1 MINUTE);

Update Statement with Case in MYSQL

I have this issue and I have investigated more than five house but find nothing :( . I have table called support.
UPDATE support s SET s.Survey_Status = 0
CASE
WHEN s.Survey_Status = 0 Then 1
WHEN s.Survey_Status = 1 Then 2
End
Where last_response < ADDDATE(CURDATE(), INTERVAL 1 DAY)
and support_waiting ="n" ;
I need to update the support table and set the survey_status =1 except the fifth row in the table will be =2 . For example, if I have survey_ status from 1 to 10 = 1 then the fifth will =2 . any idea please ??
By the way, I am working with mysql Heidi .
Thanks in Advance
You can combine user variables and MOD():
UPDATE support, (SELECT #r:=0) init
SET Survey_Status = IF(MOD(#r:=#r+1,5), 1, 2)
WHERE last_response < CURRENT_DATE + INTERVAL 1 DAY
AND support_waiting = 'n'
ORDER BY ...

Zend Db Sql Where

Hi how can I do a query like this in zf2 with zend\db\sql?
Query:
SELECT * FROM table WHERE field = $field AND data > SUBDATE(NOW(), INTERVAL 1 DAY)
In ZF2
$select = $this->sql->select();
$select->from(self::MYTABLE)
->where(array('fiels' => $field))
->where(array('data > ' => 'SUBDATE(NOW(), INTERVAL '.$lifetime.' SECOND'));
$statement = $this->sql->prepareStatementForSqlObject($select);
return $statement->execute()->current();
change the line
->where(array('data > ' => 'SUBDATE(NOW(), INTERVAL '.$lifetime.' SECOND'));
to
->where(array('data > ?' => 'SUBDATE(NOW(), INTERVAL '.$lifetime.' SECOND'));
From the code snippet, it's seen you had missed the place holder for the parameter(?), include a question mark, I had mentioned the existing line of code and the modified code for quick reference
There's no parameter there so it doesn't need to be an array. Assuming you know $lifetime is a safe, integer value, try:
->where('data > SUBDATE(NOW(), INTERVAL '.$lifetime.' SECOND)');

Convert MySQL fetch array query to redbean PHP

I have 2 buttons which execute a post operations and set a hidden variable which is used to set the MySQL query to filter the database according to date
if result = today
$query = "SELECT id,customer_name,CAST( `register_date` AS DATE ) AS dateonly,status,
DATE_FORMAT(book_date, '%m/%d/%y') FROM table WHERE book_date
BETWEEN (CURDATE() - INTERVAL 1 DAY) AND CURDATE()";
if result = week
$query = "SELECT id,customer_name,CAST( `register_date` AS DATE ) AS dateonly,status,
DATE_FORMAT(book_date, '%m/%d/%y') FROM table
WHERE book_date BETWEEN (CURDATE() - INTERVAL 7 DAY) AND CURDATE()";
I then want to use something like
$result=mysql_query($query);
while ($mytable=mysql_fetch_array($result))
{
loop and display all the information in array in a table
}
But I need the red bean equivalent of this.
The easiest way is to just paste the $query inside the sql function:
$results=R::getAll($query);
foreach($results as $row){
echo $row['id'];
}
The next way is to manually build the query.... which may just make it look sloppier in my opinion:
$results=R::$f->begin()->select('id, customer_name, CAST( register_date AS DATE ) AS dateonly,status, DATE_FORMAT(book_date, '%m/%d/%y')')->from('table')->where('book_date BETWEEN (CURDATE() - INTERVAL 1 DAY) AND CURDATE())->get();
The final way is to grab results via redbean and handle them manually:
$results=R::find('table','book_date BETWEEN (CURDATE() - INTERVAL 7 DAY) AND CURDATE()');
Then loop through the results, configuring data along the way in php.
I always use this when I have to access a lot of data from mysql:
while ($row = mysqli_fetch_array($query)) { #converts query into array
$array[] = $row;
}
$array will be a multidimensional array. $array[x][column_name] will get you your data, x being the row which you want to access it from. Hope this helped.

Codeigniter SQL Query - DATE_ADD

i'm using CodeIgniter (http://codeigniter.com/) and have a problem with an query:
select *
from mb_login_attempts
where ip_adress_hash = ?
and DATE_ADD(attempt_date,INTERVAL 30 MINUTE) > NOW()
i would like to use the following syntax:
$this->db->where('ip_adress_hash', $this->encrypt->sha1($this->input->ip_address()));
$this->db->where('DATE_ADD(attempt_date,INTERVAL 30 MINUTE) >','NOW()',TRUE);
if($this->db->count_all_results('mb_login_attempts') >= 3) {
return true;
}
If i use this code:
$sql = "select *
from mb_login_attempts
where ip_adress_hash = ?
and DATE_ADD(attempt_date,INTERVAL 30 MINUTE) > NOW()";
$val = $this->db->query($sql,$this->encrypt->sha1($this->input->ip_address()));
if($val->num_rows() >= 3) {
return true;
}
Does anyone have an idea how i get the first code get working correct?
Edit: i have change some code to the comments - but it still doesn't work ...
regards ...
Modify the last line with the TRUE FALSE value so it won't escape the NOW() function:
$this->db->where('DATE_ADD(attempt_date,INTERVAL 30 MINUTE) >','NOW()', FALSE);
EDIT:
Run this query:
$this->db->where('ip_adress_hash', $this->encrypt->sha1($this->input->ip_address()));
$this->db->where('DATE_ADD(attempt_date,INTERVAL 30 MINUTE) >','NOW()', FALSE);
$val = $this->db->get('mb_login_attempts');
EDIT2:
It Is still okay to use this form:
...
$this->db->where('ip_adress_hash', $this->encrypt->sha1($this->input->ip_address()));
$this->db->where('DATE_ADD(attempt_date,INTERVAL 30 MINUTE) >','NOW()', FALSE);
if($this->db->count_all_results('mb_login_attempts') >= 3) {
return true;
}