Question

I'm using CodeIgniter to build an application with PostgreSQL as the database layer.

I have an AJAX based search function in the application that queries multiple columns in the database for potential matches. One particular column has a type of BIGINT. In order to do a LIKE query on this column, I first need to cast it to text. Writing the SQL for this is straightforward:

 SELECT * FROM tablename WHERE columnname::text LIKE '1011%';

At the moment I cannot see a way of replicating this using CodeIgniter's Active Record as the following produces an error:

$this->db->like('columnname::text', '1011%');

Is it safe to assume that it's not possible to do this type of query in CI and should revert to $this->db->query();?

The error I receive from CI is:

A Database Error Occurred
Error Number:
ERROR: column "columnname::text" does not exist LINE 3: WHERE "columnname::text" LIKE '%'

I haven't left the error number out above, that is how it appears.

Was it helpful?

Solution 2

I just wanted to confirm that it is indeed the case that using the query method was needed.

$sql = ('SELECT * FROM tablename WHERE columname::text LIKE ?');
$query = $this->db->query($sql, array($str . '%'));

OTHER TIPS

Have you tried without the ::text like that:

$this->db->like('columnname', '1011%');

Usually you don't need to specify the datatype, unless inside specific functions or particular cases

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top