We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 7923
    • 4,213 Posts
    Hello,

    I have a code like this:

    $query = $modx->newQuery('ltMarker', array('map_id' => $map->get('id')));
    $query->select('MAX(`index`)');
    $query->prepare();
    $modx->log(modX::LOG_LEVEL_ERROR, $query->toSQL());
    $index = $modx->getValue($query);
    $modx->log(modX::LOG_LEVEL_ERROR, 'index: ['.$index.']');
    


    And that produces a log like this:

    [2013-03-26 23:42:25] (ERROR @ /index.php) SELECT MAX(`index`) FROM `marker` AS `ltMarker` WHERE `ltMarker`.`map_id` = 1 
    [2013-03-26 23:42:25] (ERROR @ /index.php) index: []
    


    If I run that SQL clause in phpmyadmin, it returns the maximum value of index column correctly:

    MAX(`index`)
    ------------
    1.0
    


    But the $modx->getValue() returns nothing. Why is that?

    I have done this based on this post by jason.


    EDIT: I resolved this by using the following, but would still like to know why the above does not work.

    $maxindex = null;
    $stmt = $modx->query('SELECT MAX(`index`) FROM '.$modx->getTableName('ltMarker').' WHERE map_id='.$map->get('id'));
    if ($stmt) {
        $maxindex = $stmt->fetchAll(PDO::FETCH_COLUMN);
    }
    // MAX column value is now in $maxindex[0]
    
    [ed. note: doze last edited this post 13 years, 6 months ago.]


      "He can have a lollipop any time he wants to. That's what it means to be a programmer."