Mel*_*Dog 1 php mysql modx content-management-system modx-revolution
我试图在ModX Revo中的片段内查询数据库:
<?php
$leadersql = "SELECT * FROM `modx_menus`";
$result = mysql_query($leadersql);
while ($row = mysql_fetch_array($result)) {
echo "hello";
};
?>
Run Code Online (Sandbox Code Playgroud)
在Evo上,这很好但在Revo中没有任何回报.
我需要以不同的方式进行设置吗?
Christian的代码确实有用,您可能需要首先定义$ rows数组:
$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>';
Run Code Online (Sandbox Code Playgroud)
如果没有: - 你确实有菜单项? - 你正在调用你的片段未缓存?
理想情况下,您将使用xPDO方法来构建数据库查询。它自动转义提供的参数,创建跨数据库类型(当前为 mysql 和 mssql)转换的查询,并且具有许多其他好处。但是,设置也比较棘手,因为您需要为自定义表创建类和映射。Bob 的指南提供了很好的信息, Lazylegs也是如此
当然,您可以实现您的特定查询,而不使用 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);
}
}
Run Code Online (Sandbox Code Playgroud)
如果您的要求很简单,还有Rowboat插件,用于跨数据库表行进行迭代。