0

以下查询返回我预期的结果:

$link=mysqli_connect('localhost','user','pass');
    if(!$link){
    echo "No connection!";
    exit();
    }

    if (!mysqli_set_charset($link, 'utf8'))
    {
      echo 'Unable to set database connection encoding.';
      exit();
    }

    if(!mysqli_select_db($link, 'database')){
        echo "No database";
    exit();
};

$res = $link->query("SELECT rules FROM xmb9d_viewlevels WHERE id=10");

while ($row = $res->fetch_array()) {
    echo " cenas = " . $row['rules'] . "\n";
};

但是,由于我使用的是 Joomla 2.5.16 并且我试图保留它的语法,所以我尝试了:

$db = JFactory::getDbo();
$query = $db->getQuery(true); 

$res = $db->query("SELECT rules FROM #__viewlevels WHERE id=10");

while ($row = $res->fetch_assoc()) {
    echo " cenas = " . $row['rules'] . "\n";
};

这是行不通的。它只显示文本“cenas =”。

这段代码有什么问题?

4

1 回答 1

0

尝试以下操作:

$db = JFactory::getDbo();

$query = $db->getQuery(true);

$query->select($db->quoteName('rules'))
 ->from($db->quoteName('#__viewlevels'))
 ->where($db->quoteName('id')." = ".$db->quote('10'));

$db->setQuery($query);

$results = $db->loadObjectList();

foreach ( $results as $result) {
   echo " cenas = " . $result->rules;
}
于 2013-11-10T23:10:45.613 回答