1

I have a url that looks like this: website.com?id=1,2

    $urlParam = $_GET['id']; 
    echo $urlParam;

$urlParam returns 1,2 as expected. However....i'm trying to use it to query a mysql database (I think that's the right phrase for what i'm doing??). This works:

    $query->where(array(
       'id:IN' => array(1,2)
    ));

This does not work:

    $query->where(array(
       'id:IN' => array($urlParam)
    ));

I don't get it! Why?? Also...I barely have any idea what i'm doing, so I'm sure there's a more efficient way to do all this but this is what I was able to come up with.

Shonna
  • 993
  • 2
  • 9
  • 12
  • Do: `var_dump($urlParam);` and `var_dump(["I'm an array"]);` and look at the output of both, especially what type they have. <- Then you should see what the difference is and where you are going wrong – Rizier123 Sep 02 '15 at 16:56
  • Doesn't look like you use plain MySQLi or PDO. Which framework (or helper class) do you use? – Charlotte Dunois Sep 02 '15 at 16:59
  • I'm creating a snippet in MODX, Google says it uses a framework called xPDO – Shonna Sep 02 '15 at 17:31

3 Answers3

3

Because 1,2 is not seen as a comma delimited value by MySQL. It see it as a string with a value of "1,2". You have no rows matching that value so you get no results.

See this answer for how to work around this.

Community
  • 1
  • 1
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • I now understand why it was not working. That makes sense. The workaround was a bit too complex for me, though. Luckily someone had an easier solution – Shonna Sep 02 '15 at 17:48
1

You could set it so that rather than a string, it's an array. Ie.

<form method="GET">
<input name="test[]" value="1" />
<input name="test[]" value="2" />
<input type="submit">
</form>

<?php
print_r($_GET);
?>

This will show up in your url as:

www.myurl.com?test%5B%5D=1&test%5B%5D=2
Robbie
  • 700
  • 2
  • 6
  • 18
1

To convert string into array you can use explode function:

$query->where(array(
   'id:IN' => explode(',',$urlParam)
));
Alex
  • 16,739
  • 1
  • 28
  • 51