Search code examples
phpmysqlcontent-management-systemmodxmodx-revolution

Running custom database queries in ModX Revo


I am trying to query the database within a snippet within ModX Revo:

<?php

$leadersql = "SELECT * FROM `modx_menus`";

$result = mysql_query($leadersql);

while ($row = mysql_fetch_array($result)) {
  echo "hello";
};

?>

On Evo this worked fine but returns nothing in Revo.

Do I need to set it up differently?


Solution

  • Christian's code does work you may need to define the $rows array first:

    $leadersql = "SELECT * FROM `modx_menus`";
    
    $query = $modx->query($leadersql);
    
    $rows = array();
    
    if ($query) {
        // loop through the result set and inspect one row at a time
        while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
    
            array_push($rows, $row);
    
        }
    }
    
    echo '<br /><br /><pre>';
            print_r($rows);
    echo '</pre>';
    

    if not: - you do have menu items defined? - you are calling your snippet uncached?