PDO arrays and using IN in select statement

Do yourself a big favor and use the Doctrine Database Abstract Layer which is a thin wrapper for PDO. Among many other things, DBAL allows passing an array to an IN clause bypassing a great deal of nonsense.

Ideally I need to try and fix what I have above for now, as i have been given this to sort out in between other work and my manager isn’t going to be happy me moving onto a different path again.

But I have bookmarked that link as it does look very interesting.

With my code above I have an issue now I think around the while ($t = $f->fetch()) {

As I echo’d out $f and $regionsArray[0] and [1], and all is fine.

Very frustrating

Even when I take all this out and just put the id’s actually into the select statement the while loop still doesnt seem to want to work, by this I mean

if ($sqlregion!="" && $sqlstar=="" && $sqlcategory==""){
$f = $pdo->prepare("select DISTINCT(tbl_hotels.IdCat_Hot) from tbl_hotels WHERE (Act_Hot='1') AND (IdCat_Hot IN (1,2,3,4,5)) AND (IdCntry_Hot='9') AND (IdRsrt_Hot IN (198)) order by IdCat_Hot ASC");
while ($t = $f->fetch()) { ?>
<input type="checkbox" name="star[]" value="<?php echo $t['IdCat_Hot']?>" onClick="javascript:checkRefresh()" <?php echo (isset($_REQUEST['star']) && in_array($t['IdCat_Hot'], $_REQUEST['star'])) ? 'checked="checked"' : '' ?> class="inline" />
<?php echo $t['IdCat_Hot']?> Star

Ok might be something, I added below

$f = $pdo->prepare("select DISTINCT(tbl_hotels.IdCat_Hot) from tbl_hotels WHERE (Act_Hot='1') AND (IdCat_Hot IN (1,2,3,4,5)) AND (IdCntry_Hot=?) AND (IdRsrt_Hot IN (".$placeHoldersRegion.")) order by IdCat_Hot ASC");
$selectCountryb = array($selectCountry);
array_unshift($regionsArray, $selectCountryb);  
$f->execute(print_r($regionsArray));

and got as an output

Array ( [0] => 9 [1] => 198 )
Warning: PDOStatement::execute() expects parameter 1 to be array, boolean given in

does that look sensible to you?

:tired_face:

I’m starting to have non-sensible thoughts with this now, so I’m looking for clues. Thought that might help sorry

I am though getting - Notice: Array to string conversion with this line

$f->execute($regionsArray);

Just to just compare this block of code works perfectly, so the way Im using execute is causing problems, and I’m not sure at all

if ($sqlregion!="" && $sqlstar=="" && $sqlcategory==""){
$s = $pdo->prepare("select DISTINCT(tbl_hotels.IdCat_Hot), IdCntry_Hot from tbl_hotels WHERE IdCat_Hot IN (1,2,3,4,5) and (Act_Hot='1') and (IdCntry_Hot=:selectCountry) order by IdCat_Hot ASC");
$s->bindParam(":selectCountry", $selectCountry);
$s->execute();   
while ($b = $s->fetch()) { ?>
<input type="checkbox" name="star[]" value="<?php echo $b['IdCat_Hot']?>" onClick="javascript:checkRefresh()" <?php echo (isset($starArray['star']) && in_array($b['IdCat_Hot'], $starArray['star'])) ? 'checked="checked"' : '' ?> class="inline" />
<?php echo $b['IdCat_Hot']?> Star

I have to go now, but this is where I’m at and the error if it helps someone help me

$selectCountryb = array($selectCountry);
$regionsArray = array_unshift($regionsArray, $selectCountryb);
$f->execute($regionsArray);
while ($t = $f->fetch()) {

Warning: PDOStatement::execute() expects parameter 1 to be array, integer given in

the above $selectCountry = 9 and $regionsArray = 198

cf. http://php.net/array-unshift

Return Values

Returns the new number of elements in the array.

OP, stop what your doing and step back for a minute and read this tutorial on PDO. https://phpdelusions.net/pdo

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.