开发者

Manually add data to MySQL query array

I've just started using Codeigniter, and am loving MVC. I have a simple mysql query in a model that returns an array of rows to my controller.

$query = $this->db->get('shows');
return $query->result();

Date information is stored in the database as mysql date 开发者_运维百科(yyyy-mm-dd) and I explode to get month mm and day dd.

What I'm wondering is, is there any way to manually add the variables for month and day of each row to the query result using a simple foreach? I know I could manually add each database field's value to an array, and include the month and day variables, but I'm hoping there's a simpler way of inserting them into the already existing array created by the query.


@slier had the code, but below is transformed to codeigniter

$this->db->select("*, MONTH(date_column) as date_month, DAY(date_column) as date_day");
$query = $this->db->get('shows');
return $query->result();


Ok im not familiar with codeigniter, but if you want to get month and day in raw mysql you can simply do something like below

$sql = 'SELECT MONTH(date) AS month, DAY(date) AS day FROM shows';
$result = $this->db->query($sql);

Im assuming your date column is in date dataype

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜