Passing MySQL's CURRENT_TIMESTAMP to Zend_DB update statement - php

How do I pass mysql's CURRENT_TIMESTAMP when using Zend_DB's update statement? The following doesnt seem to be working.
I have something like this:
$update = array(
'Name' => 'John',
'DT_Modified' => 'CURRENT_TIMESTAMP'
);
$db->update('usertable', $update );
to run a query that is represented like this:
UPDATE usertable SET Name='John', DT_Modified = CURRENT_TIMESTAMP

Try using Zend_Db_Expr to avoid unnecessary quoting:
$update = array(
'Name' => 'John',
'DT_Modified' => new Zend_Db_Expr('CURRENT_TIMESTAMP')
);
$db->update('usertable', $update );

Related

insert current datetime to custom wordpress datetime column

I have an issue with inserting current datetime to custom wordpress datetime column. I've used curent_time function I also used date('Y-m-d H:m:s') as value for the column but it did not work.
I will provide my code for any suggestions:
global $wpdb;
$load_rec_table = $wpdb->prefix.'load_rec';
// Insert into load record table
$wpdb->insert($load_rec_table,array(
'rec_date' => current_time('mysql'),
'total_load' => $_POST['totalPower'],
'total_gap' => $_POST['totalGap'],
'carriage' => $_POST['carriage']
));
You can use NOW() or CURRENT_TIMESTAMP() functions of Mysql
global $wpdb;
$load_rec_table = $wpdb->prefix.'load_rec';
// Insert into load record table
$wpdb->insert($load_rec_table,array(
'rec_date' => NOW(),
'total_load' => $_POST['totalPower'],
'total_gap' => $_POST['totalGap'],
'carriage' => $_POST['carriage']
));

Wordpress update query not working

I have an update query which is not working for me. I am able to make selects quite happily on the same page but I cannot get an update statement to work.
The table is not a part of wordpress so Im wondering if that could be it or if I have just got something wrong.
$query = "UPDATE login_count SET `count` = '100' WHERE `user_id` = $userID ";
$insrt = $wpdb->query($query);
Try this,
$insrt = $wpdb->update(
'login_count', //table_name
array('count'=>'100'),//data
array('user_id'=>$userID),//where
array('%s'),//data format
array('%s')
);
$insrt = $wpdb->update(
'login_count', //table_name
array(
'count' => '100', // string
),
array( 'user_id' => $user_id ), //Where Condition
array(
'%d', // value1
),
array( '%d' )
);
Try this ..user_id = {$userID} ";
Edit
See: Use a $variable inside a SQL string?

codeigniter how to update a single attribute in a tuple

i want to update a single column (tuple) within attribute in table. i have the array shown below in controller.
$promotion_info = array(
'category' => $this->input->post('category'),
'rank' => $this->input->post('rank'),
'date_of_start' => $date_of_promotion
);
i passed it to model as shown below:-
$this->insert_model->form_promotion($fno, $promotion_info);
on model i want to update column with date_of_promotion. Then inserting a new row (attribute) with the value contained in array.
how can i update that column with the single value in array
Following docs:
$this->db->set('field', 'field+1', FALSE);
$this->db->where('id', 2);
$this->db->update('mytable'); // gives UPDATE mytable SET field = field+1 WHERE id = 2
$this->db->set('field', 'field+1');
$this->db->where('id', 2);
$this->db->update('mytable'); // gives UPDATE `mytable` SET `field` = 'field+1' WHERE `id` = 2
In your case it could be something like:
$array = array(
'category' => $name,
'rank' => $title,
'date_of_start' => $status
);
$this->insert_model->form_promotion($fno, $data);
In model:
$this->db->set($data);// this is case if you need to update all 3 values
//if you need to update only one value from the array than
//$this->db->set('date_of_start', $data['date_of_start']);
$this->db->where('date_of_start', "SOME_EXISTING_DATE_FROM_TABLE");
$this->db->insert('mytable');//

CakePHP Mysql query with AND condition

I am running a Mysql query in CakePHP but it is not outputting any result though no error is displayed.
Mysql query is this:
SELECT `isbn`, `title`, `author_name` FROM `books` WHERE `author_name` = 'William Rice' AND `title` LIKE '%Course%' ORDER BY `title` ASC
and it outputs results.
And I have written this in CakePHP find function:
$books = $this->Book->find('all', array('fields'=>array('Book.isbn', 'Book.title', 'Book.author_name'),
'conditions'=>array('Book.author_name'=>'William Rice',
'AND'=>array('Book.title'=>'LIKE %Course%')),
'order'=>'Book.title ASC',
));
But the query doesn't run. Is AND condition specified like this? Please have a look at it and tell me where is it wrong. Thanks.
Do not use AND array:
You have to change your query as given below:
$books = $this->Book->find('all',
array('fields'=>array('Book.isbn', 'Book.title', 'Book.author_name'),
'conditions'=>array('Book.author_name'=>'William Rice',
'Book.title LIKE'=>'%Course%'),
'order'=>'Book.title ASC'
));
$books = $this->Book->find('all', array(
'fields' => array('Book.isbn', 'Book.title', 'Book.author_name'),
'conditions' => array(
'Book.author_name'=>'William Rice',
'Book.title LIKE'=>'%Course%'
),
'order'=>'Book.title ASC',
));
You have done only one mistake , Remove 'AND' from your query, after removing 'AND' your query will be like below query.
$books = $this->Book->find('all', array(
'fields' => array('Book.isbn', 'Book.title', 'Book.author_name'),
'conditions' => array(
'Book.author_name'=>'William Rice',
'Book.title LIKE'=>'%Course%'
),
'order'=>'Book.title ASC',
)
);

how to use LIKE with conditions in sqlite or mysql with lithium recordset

so I can look for concrete values by doing
$recordset= Model::find('all', array(
'conditions' => array(
'condition' => $somevalue
)
))
however, what do I do if I want to match on a partial value?
right now, I've resorted to writing the query myself, a la:
$abc = Connections::get('default')->
read('SELECT * FROM myTable WHERE condition LIKE "%partial string%"');
Here's how I do an SQL 'like' search:
$user = User::find('all', array(
'conditions' => array(
'first_name' => array('like' => '%yeun%'))
)
);
'like' being the keyword, there.
That would generate a query like:
SELECT * FROM `users` AS `Users` WHERE (`first_name` like '%yeun%');
Hope that helps.

Categories