We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 40122
    • 330 Posts
    This has got me completely staggered.

    I have an sql query that retrieves 1 result. I want to make an if statement that says if the result is retrieved do this, else do something else.

    I have tried:

     $sql = "SELECT * FROM `items` WHERE `teamid` = $teamid ORDER BY `item_id` DESC LIMIT 1";
     $sql = $modx->query($sql);
    
        if (is_object($sql)) {
                while ($sqlrow = $sql->fetch(PDO::FETCH_ASSOC)) {
                    $lastitem = $sqlrow['itemname'];
                }//WHILE
            }//if
            else {
                $lastitem = "NA";
            }//ELSE


    but not matter what I do the IF statement is triggered.

    I have also tried using isset() and even strlen().

    Does anyone know a way to say
    'If the SQL statment returned no results from the database'
    ?

    Is the only way to do this with a count?

    Many thanks in advance
      • 4172
      • 5,888 Posts
      very clean with pure XPDO

      $classname = 'yourXPDOclass';
      
      $c = $modx->newQuery($classname);
      $c->where(array('teamid'=>$teamid));
      $c->sortby('item_id','DESC');
      $c->limit('1');
      
      if ($object = $modx->getObject($classname,$c)){
          $lastitem = $object->get('itemname');   
      }else{
          $lastitem = 'NA';
      }
      
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!