6

The code for the query I'm trying to run is

$query = $this->db->select("*")->from('items')->like("replace(name, '=', ' ')", "foo bar", 'both')->get();

$items = $query->num_rows();

Mysql query string is

SELECT replace(name, ' = ', ' ') FROM `items`

What the code is doing is adding a blank space before and after "=" when compiling the query resulting in " = " which returns no results as there are no items with " = " in their names, only "=".

foo=bar, replace(name, '=', ' ') returns 1 result.
foo = bar, replace(name, ' = ', ' ') returns 0 results.

The version of CodeIgniter that I'm using is: 3.0.6

10
  • Not sure why you would want to do this in mysql? Its not a great approach. You would maybe want to do a str replace on the string before/after you do a query to the db.
    – Philip
    May 5, 2016 at 0:52
  • @Philip The reason I'm doing this is that what I'm looking to replace is not always the same thing, it works perfect with ".", "-" or "+" together but with "=" the problem happens.
    – Joscplan
    May 5, 2016 at 0:59
  • give me an example so I can offer a solution.
    – Philip
    May 5, 2016 at 1:04
  • $query = $this->db->select("*")->from('items')->like("replace(replace(replace(name, '=', ' '), '.', ' '), '-', ' ')", "foo bar", 'both')->get(); And you have this items on your table foo = bar, foo . bar, foo - bar
    – Joscplan
    May 5, 2016 at 1:09
  • I understand the query you are trying to run. I am just not sure WHY you are doing it that way. It does not make any sense. It would make more sense to just query the db with your like filter, then iterate over each element and replace the string
    – Philip
    May 5, 2016 at 1:15

4 Answers 4

7
+25

I have tested this on my copy of CodeIgniter and when formatted like this it works just fine:

$items = $this->db->like("replace(name, '=', ' ')", "foo bar", 'both')->get('items')->num_rows();

A few notes:

  1. You don't need from(), you can just slip the table into the get() method.
  2. You can chain your methods so you only need to assign one variable.
  3. Omitting the select() method implies select('*').
2

Your syntax

$query = $this->db->select("*")->from('items')->like("replace(name, '=', ' ')", "foo bar", 'both')->get();

as shown here in fact is producing this query

SELECT * FROM `items` WHERE replace(name, '=', ' ') LIKE '%foo bar%';

If there is by accident one more space between foo and bar it fails.

Try this alternative query

SELECT * FROM `items` WHERE replace(name, '=', ' ') LIKE '%foo% bar%';

CodeIgniter

$query = $this->db->select('*')->from('items')->like("replace(name, '=', ' ')", "foo% bar", 'both')->get();

OR this query even better

SELECT * FROM `items` WHERE CONCAT(TRIM(SUBSTRING_INDEX(name, '=', 1)),' ',TRIM(SUBSTRING_INDEX(name, '=', -1))) LIKE '%foo bar%';

Don't use this

SELECT * FROM `items` WHERE replace(name, '=', ' ') LIKE '%foo%bar%';

The results of those queries are shown here

Either you could have a query that removes leading/trailing space (Example)

2

I cant find an method call replace() in associated with codeigniter or php.

Try str_replace(find,replace,string)

Example

$one = 'as = bb = cc=dd ';
$var = str_replace('=', ' ', $one);
echo $var;

phpfiddle Preview

And don't refactor your code inside your query. Make it clean and pass it to query. Its easy to track errors too.

1
  • 1
    replace method is available in MySQL
    – A J
    May 16, 2016 at 10:05
0

I used

$this->db->query()

instead of

$this->db->select();
$this->db->order_by();
$this->db->get();

and it worked for me.

My query was:

$records = $this->db->select(
"id,( select count(*) from 
( select 
        unnest(string_to_array(mycol,',')) v
        from mytable ) t where v::int > 0) as total",
FALSE)->order_by("col1")->get("mytable1")->result_array();

Issue was, it was adding an extra space at string_to_array(mycol,', ') which was giving error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.