SELECT rows WHERE a string is found in a serialized column value using CodeIgniter's query builder
06:06 14 Jun 2019

I am adding a where clause on the key of a serialized array field (I am using PHP/MySQL), but the SQL is not working and emits a parse error.

Error:

Parse error: syntax error, unexpected ''Employer'' (T_CONSTANT_ENCAPSED_STRING) in....

Serialized array in the database:

a:2:{s:8:"Employer";s:9:"sanjayEMP";s:7:"Section";s:10:"production";}

CodeIgniter model method:

$this->db->select("*");
$this->db->select("emplyee_details");
$this->db->where("status", 1);
$this->db->where("ValueDetails", "REGEXP '.*"Employer";s:[0-9]+:"jack".*'");
$this->db->get()->result_array();
  • ValueDetails is a field of serialized array in database table.
  • Employer is the array key of the serialized field in the database table.

If anyone has a great idea how to add the where clause on array key of any specialized field, please give me suggestions.

php regex codeigniter serialization query-builder