We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 34127
    • 135 Posts
    Hi, I’m having some trouble rewriting a couple of my SQL queries with xPDO. I have a query that looks something like this:
    SELECT * FROM items WHERE item_id IN(1,2,3)


    And I’ve tried rewriting it like this:

    $c = $this->modx->newQuery( 'itemdbItems' );
    $c->where( array( 'item_id:IN' => '1,2,3' ) );
    $items = $this->modx->getCollection( 'itemdbItems', $c );
    


    But it doesn’t seem to work, and I get the following error in my MODx error log:

    [2008-08-28 18:01:37] (ERROR @ /revolution/index.php) Error 42000 executing statement: 
    Array
    (
        [0] => 42000
        [1] => 1064
        [2] => You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''1,2,3'' at line 1
    )


    What am I doing wrong here?
      • 7923
      • 4,213 Posts
      Try this:
      $items= $this->modx->getCollection('itemdbItems', array ('item_id:IN' => '(1,2,3)'));
      

      or:
      $c = $this->modx->newQuery( 'itemdbItems' );
      $c->where(array('item_id:IN' => '(1,2,3)' ));
      $items = $this->modx->getCollection( 'itemdbItems', $c );
      

      So just add those brakets around the IN value list.


        "He can have a lollipop any time he wants to. That's what it means to be a programmer."
        • 34127
        • 135 Posts
        It still doesn’t want to cooperate for some reason:

        [2008-08-28 19:00:07] (ERROR @ /revolution/index.php) Error 42000 executing statement: 
        Array
        (
            [0] => 42000
            [1] => 1064
            [2] => You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''(1,2,3)'' at line 1
        )


        Odd, it looks like it’s quoting the group of values for some reason...
        • This is because I’m trying to use prepared statement bindings when you use the array(’field:OPERATOR’ => ’value’) condition syntax, and unfortunately, there is no valid PDO_PARAM_TYPE for this kind of value; PDO is actually seeing that the value is a PHP string and forcing the quoting to occur.  For now you’ll have to write:
          <?php
          $c = $this->modx->newQuery( 'itemdbItems' );
          $c->where('item_id IN (1,2,3)');
          $items = $this->modx->getCollection( 'itemdbItems', $c );
          ?>

            • 34127
            • 135 Posts
            Quote from: OpenGeek at Aug 29, 2008, 12:27 AM

            This is because I’m trying to use prepared statement bindings when you use the array(’field:OPERATOR’ => ’value’) condition syntax, and unfortunately, there is no valid PDO_PARAM_TYPE for this kind of value; PDO is actually seeing that the value is a PHP string and forcing the quoting to occur. For now you’ll have to write:
            <?php
            $c = $this->modx->newQuery( 'itemdbItems' );
            $c->where('item_id IN (1,2,3)');
            $items = $this->modx->getCollection( 'itemdbItems', $c );
            ?>


            Ahh, that makes sense! Thanks a lot, that worked perfectly. laugh
              • 7923
              • 4,213 Posts
              Has the :in operator worked in some revision of xPDO? I remembered that I have used it at somepoint succesfully.. but I use very old xPDO revision though. Or I remember wrong.


                "He can have a lollipop any time he wants to. That's what it means to be a programmer."
              • Quote from: doze at Aug 29, 2008, 12:51 PM

                Has the :in operator worked in some revision of xPDO? I remembered that I have used it at somepoint succesfully.. but I use very old xPDO revision though. Or I remember wrong.
                Yeah, I was thinking this too doze, but so much has changed. Might work if you are not running native PDO (i.e. in PHP 4, or 5.0.x), or running the latest versions of PDO perhaps got more strict about bindings, or maybe prior to xPDOQuery being introduced it did work and I broke the capability some how.

                Feel free to post a bug report to Jira for xPDO to remind me to make it work somehow. wink