phpmysqlregexcodeigniter

Codeigniter 2.1 - MySQL and regexp


I have code like this:

   function mgl_get_by($col,$id)
            {    
            $this->db->select('*');
            $this->db->from('global_info');
            $this->db->join('ad', 'id_ad = id_global_info');
            $this->db->where('info_type_id', 1);
            $this->db->where($col, $id);
            $this->db->or_where("$col REGEXP $id");
            $this->db->order_by('paid', 'desc');
            $this->db->order_by('date_created', 'desc');  
            $q = $this->db->get();        
            return $q = $q->result_array();   
    }

$col is name of the row in the table, and $id is unique id for the query. In most cases it is unique number, but I have date row in this table, and in the table it is inserted as timestamp, but when performing query it need to select all matches for the selected day (for example it needs to select all data from 2012-06-11). How can I do this (I am not familiar with REGEXP)?


Solution

  • I don't think you need RegExp here - LIKE would be just fine. If your date is stored as timestamp, you should do FROM_UNIXTIME($col) LIKE '2012-06-11'

    EDIT: Although, it is not efficient. Better way is to compare timestamp as an integer:

    $col >= UNIX_TIMESTAMP('2012-06-11 00:00:00') AND $col < UNIX_TIMESTAMP('2012-06-12 00:00:00')