1

我正在尝试构建一个片段,它将显示我的投资组合的自定义页面标题。我遇到的问题是我的代码只返回“else”,但是当我在 MySql 中运行查询时,我得到name.

我究竟做错了什么?

<?php
// Show All Errors
error_reporting(E_ALL);
ini_set('display_errors', '1');

$getID = $modx->quote($getID);

$ret = '';
$qry = "SELECT `name` FROM `modx_gallery_items` WHERE REPLACE(LOWER(`name`), ' ', '-') = $getID;";

$result = $modx->query($qry);
if ($result) {
    $row = $result->fetch(PDO::FETCH_ASSOC);
    if($row){
        $ret = 'o7th Web Design &raquo; Portfolio &raquo; ' . $row['name'];
    }else{ //It's showing this one on the page, yet the same query in MySQL returns `name`
        $ret = 'o7th Web Design &raquo; Portfolio &raquo; Our Portfolio' . $qry;
    }
    unset($row);        
}else{
    $ret = 'o7th Web Design &raquo; Portfolio &raquo; Our Portfolio' . $qry;
}

// Return everything
echo $ret;
?>
4

3 回答 3

2

发现问题。

$row = $result->fetch(PDO::FETCH_ASSOC);

需要改为:

$row = $result->fetchAll(PDO::FETCH_ASSOC);

然后我可以循环返回的数组,或者按索引返回项目:$row[0]['name']

于 2013-01-02T15:45:55.963 回答
0

你绝对不知道如何在 modx 革命中进行查询。请阅读http://bobsguides.com/revolution-objects.html。此代码的正确示例如下所示 -

// add class to work with gallery extra
$gallery = $modx->getService('gallery','Gallery',$modx->getOption('gallery.core_path',null,$modx->getOption('core_path').'components/gallery/').'model/gallery/',$scriptProperties);
if (!($gallery instanceof Gallery)) return '';

// get id of album
$getID = (int) $modx->getOption('getID',$scriptProperties,false);
if (empty($getID)) return 'no id';

// make query
$c = $modx->newQuery('galAlbum')
$c->where(array(
    'id' => $getID,
));
$item = $modx->getObject('galAlbum',$c);

// get result
if (!empty($item)) {
    $output = 'o7th Web Design &raquo; Portfolio &raquo; ' . $item->get('name');
}
else {
    $output = 'o7th Web Design &raquo; Portfolio &raquo; Our Portfolio' . ' empty';
}

return $output;
于 2013-01-02T14:52:33.887 回答
-1

这是问题吗?

$qry = "SELECT `name` FROM `modx_gallery_items` WHERE REPLACE(LOWER(`name`), ' ', '-') = $getID;";

应该:

$qry = "SELECT `name` FROM `modx_gallery_items` WHERE REPLACE(LOWER(`name`), ' ', '-') = ".$getID.";";

看起来您的查询将搜索 '$getID' 而不是它的实际值

于 2013-01-02T14:44:48.337 回答