1

This is the code (I'm using Codeigniter):

$sql = 'SELECT * FROM foo WHERE bar IN (?)';

$query = $this->db->query($sql, array($values));

So $values is an array of strings that I want to add in the SQL statement where the "?" is. When I try this I get an "Array to string conversion" error. Is there any way I can add the values of the $values array as comma separated strings into the SQL statement?

7
  • Try this: $query = $this->db->query($sql, implode(",",$values)); Commented Feb 12, 2015 at 7:48
  • that adds them all as one string so it won't work Commented Feb 12, 2015 at 7:50
  • I need each value to be a separate string so it will work in the WHERE IN statement Commented Feb 12, 2015 at 7:51
  • What about this? Try : $query = $this->db->query($sql, $values ); Commented Feb 12, 2015 at 7:51
  • that's an array to string conversion Commented Feb 12, 2015 at 7:54

2 Answers 2

2

Why don't you use simple codeigniter way

$this->db->from('foo');
$this->db->where_in('bar',$values);
$query=$this->db->get();

This will produce what you exactly want

Update
If you are strict with your way you need to produce the $sql this way

$sql = 'SELECT * FROM foo';
    if(is_array($values)&&sizeof($values)>0)
    {
        $sql.=' WHERE bar IN (';
        foreach($values as $key=>$value)
        {
            if($key==0)
            {
                $sql.='?';
            }
            else
            {
                $sql.=',?';
            }
        }
        $sql.=')';
    }       

    $query=$this->db->query($sql,$values);
Sign up to request clarification or add additional context in comments.

6 Comments

i'm building the query statement myself, it's a complicated query with joins so I can't use that
I saw your updated answer, the problem is I have other placeholders besides the values array. Something like this WHERE foo = ? AND bar IN (?) and $query=$this->db->query($sql, array($value1, $values));
it will work if your $value1,$value in order.You need to produce ? and remember ? will contain string not array.as example $sql = 'SELECT * FROM foo where bar IN(?,?,?) and id=?;$query=$this->db->query($sql,array(1,2,3,4)); It will produce SELECT * FROM foo where bar IN('1','2','3') and id='4'
thanks your way worked, do you see the other answer with 2 upvotes? Do you know how I can get that way working with placeholders? It works without placeholders but when I use it with placeholders it escapes/adds slashes to the quotes that were added with implode
Other answer is not wrong that's why he got upvote.Unfortunately It does not meet your requirement. You need to produce ? with the same number of your $values size. And you need to do it using for loop or array_map or manually or any other way.If you see my previous comment's example and update answer you will get some idea how you can do it.
|
2

You should pass string with comma separated in query. for that use implode function of php.

Do like this:

$values = array('bar1', 'bar2', 'bar3');
$string = "'".implode("',", $values)."'";

Then pass string in the query,

$sql = 'SELECT * FROM foo WHERE bar IN ($string)';
$query = $this->db->query($sql);

echo '<pre>'; print_r($query->result_array());

You will data in $query variable.

Let me know for further help if needed.

2 Comments

This answer doesn't work with my code (with placeholders) because it's escaping the quotes and adding slashes
Can someone please help on this its something like same learn.microsoft.com/en-us/answers/questions/820948/…

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.