Q:

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?

A:

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? 

A:

Ideally, you would use the xPDO method for building database queries. It automatically escapes supplied arguments, creates queries that translate across database types (currently mysql & mssql), and has many other benefits. However, it's also trickier to set up as you need to create classes and maps for your custom table. Bob's guides has good info, as does Lazylegs 
But of course you can implement your particular query, without using XPDO:
$leadersql = "SELECT * FROM `modx_menus`";
$query = $modx->query($leadersql);
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);
    }
}

And there's also the Rowboat add-on, for iterating across database table rows - if your requirements are straightforward.

A:

Why not use the built in xPDO methods:
$menus = $modx->getCollection('modMenu');
if ($menus) {
    foreach ($menus as $menu) {
        echo $menu->get('text');
    }
}

