【问题标题】:$db->loadObjectList and mysql_fetch_array error$db->loadObjectList 和 mysql_fetch_array 错误
【发布时间】:2013-08-12 12:37:31
【问题描述】:

我正在开发一个 joomla 模块,我为 db 连接编写此代码,然后从 db 中获取数据并将其打印到表中

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

 if($q == ""){

 $query
  ->select(array('Stune_code,Stune_name,Stune_artist'))
  ->from('my_table')
  ->where('sub_cat_id = "4"')
  ->limit('$start, $per_page');

$query_pag_data = $db->loadObjectList();

$msg = "<table class='show-rslt'><tr>
<th class='tbl-header'>Song Title</th>
<th class='tbl-header'>Artist</th>
<th class='tbl-header'>Code</th>
</tr>";
while ($row = mysql_fetch_array($query_pag_data)) {

$msg .= "<tr>";
$msg .= "<td class='title'>" . htmlentities($row['Stune_name']) . "</td>";
$msg .= "<td class='title'>" . htmlentities($row['Stune_artist']) . "</td>";
$msg .= "<td class='title'>" . htmlentities($row['Stune_code']) . "</td>";
$msg .= "</tr>";
}
$msg .= "</table>";
$msg = "<div class='data'><ul>" . $msg . "</ul></div>"; // Content for Data

但它给了我这个警告“警告:mysql_fetch_array() 期望参数 1 是资源,C 中给出的数组:”

然后我调试它并使用 print_r() 它给了我类似的结果

Array 
(
[0] => stdClass Object
    (
        [Stune_code] => 501348
        [Stune_name] => xxx
        [Stune_artist] => abc
    )

[1] => stdClass Object
    (
        [Stune_code] => 501351
        [Stune_name] => xxx
        [Stune_artist] => abc
    )

[2] => stdClass Object
    (
        [Stune_code] => 5011727
        [Stune_name] => xxx
        [Stune_artist] => asd
    )
...
...

我哪里错了,我应该怎么做才能得到正确的结果

【问题讨论】:

标签: php joomla


【解决方案1】:

您已经通过 Joomla 数据库对象加载了数据,因此无需再次尝试获取。只需执行 foreach 即可遍历数组。

首先将您的调用更改为loadAssocList()(需要,因为您尝试使用关联数组而不是对象来访问数据):

$query_pag_data = $db->loadAssocList();

然后将循环改为:

foreach($query_pag_data as $row) {
    $msg .= "<tr>";
    $msg .= "<td class='title'>" . htmlentities($row['Stune_name']) . "</td>";
    $msg .= "<td class='title'>" . htmlentities($row['Stune_artist']) . "</td>";
    $msg .= "<td class='title'>" . htmlentities($row['Stune_code']) . "</td>";
    $msg .= "</tr>";
}

Joomla 数据库对象应该用于所有数据库工作,不要尝试使用任何mysql_* 函数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-29
    • 2012-08-19
    • 2012-07-31
    • 2023-03-08
    相关资源
    最近更新 更多