$ db-> loadObjectList和mysql_fetch_array错误 [英] $db->loadObjectList and mysql_fetch_array error

查看:101
本文介绍了$ db-> loadObjectList和mysql_fetch_array错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究joomla模块,我编写了用于数据库连接的代码,然后从db获取数据并将其打印在表中

i am working on a joomla module and i write this code for db connection and then fetch data from db and print it in a table

 $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中给出数组:"

but it gives me this warning " Warning: mysql_fetch_array() expects parameter 1 to be resource, array given in C:"

然后我调试它并使用print_r()它给我类似

then i debug it and use print_r() it gives me result like

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
    )
...
...

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

where am i wrong and what should i do to get the proper result

推荐答案

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

You've already loaded the data via the Joomla database object, so there is no need to try to fetch again. Just do a foreach to loop over the array.

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

First change your call to loadAssocList() (needed because you're trying to access the data using an associative array instead of object):

$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_*函数.

The Joomla database object should be used for all database work, don't try to use any of the mysql_* functions.

这篇关于$ db-&gt; loadObjectList和mysql_fetch_array错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆