Re: Array to string conversion error help needed
| From: | Shawn McKenzie | Date: | Tue, 08 Mar 2016 19:40:49 +0000 |
| Subject: | Re: Array to string conversion error help needed | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-325453@lists.php.net to get a copy of this message | ||
On Tue, Mar 8, 2016 at 12:56 PM, dealtek@gmail.com <dealtek@gmail.com>
wrote:
> newbie mysql select help needed.
>
>
> this works fine...
>
> $sth = $pdo->prepare("SELECT * FROM table1 where id IN (1,2,3)"
>
>
>
> but with an array like...
>
> $mylist = array("1","2","3");
> or
> $mylist = array(1,2,3);
>
> these fail...
> $sth = $pdo->prepare("SELECT * FROM table1 where id IN $mylist"
> or
> $sth = $pdo->prepare("SELECT * FROM table1 where id IN ($mylist)"
>
>
> I get error : Array to string conversion...
>
> Q: Can anyone help me fix this?
>
>
> --
> Thanks,
> Dave - DealTek
> dealtek@gmail.com
> [db-16]
>
>
If they are numbers:
$mylist = implode(',', $mylist);
If text you will need:
$mylist = "'".implode('','', $mylist)."'";
But in either case the original array needs to be run through an
escape_string() function to be safe, possibly using array_map().