$db->loadObjectList 和 mysql_fetch_array 错误
我正在开发一个 joomla 模块,我为 db 连接编写了这段代码,然后从 db 中获取数据并将其打印在表中
$db = JFactory::getDBO();$query = $db->getQuery(true);if($q == ""){$查询->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 = "<th class='tbl-header'>歌曲名称</th><th class='tbl-header'>艺术家</th><th class='tbl-header'>代码</th></tr>";而 ($row = mysql_fetch_array($query_pag_data)) {$msg .= " ";$msg .="".htmlentities($row['Stune_name']) ."</td>";$msg .=" ".htmlentities($row['Stune_artist']) ."</td>";$msg .=" ".htmlentities($row['Stune_code']) ."</td>";$msg .= "</tr>";}$msg .="
";$msg = "".$味精."</ul></div>";//数据内容
但它给了我这个警告警告:mysql_fetch_array()期望参数1是资源,数组在C中给出:"
然后我调试它并使用print_r()它给了我像
这样的结果数组([0] =>标准类对象([Stune_code] =>501348[Stune_name] =>xxx[Stune_artist] =>美国广播公司)[1] =>标准类对象([Stune_code] =>501351[Stune_name] =>xxx[Stune_artist] =>美国广播公司)[2] =>标准类对象([Stune_code] =>5011727[Stune_name] =>xxx[Stune_artist] =>阿斯达)......
我哪里错了,我应该怎么做才能得到正确的结果
解决方案您已经通过 Joomla 数据库对象加载了数据,因此无需再次尝试获取.只需执行一个 foreach
来循环数组.
首先将您的调用更改为 loadAssocList()
(需要,因为您正在尝试使用关联数组而不是对象访问数据):
$query_pag_data = $db->loadAssocList();
然后将循环改为:
foreach($query_pag_data as $row) {$msg .= "";$msg .="".htmlentities($row['Stune_name']) ."</td>";$msg .=" ".htmlentities($row['Stune_artist']) ."</td>";$msg .=" ".htmlentities($row['Stune_code']) ."</td>";$msg .= "</tr>";}Joomla 数据库对象应该用于所有数据库工作,不要尝试使用任何 mysql_*
函数.
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
but it gives me this warning " Warning: mysql_fetch_array() expects parameter 1 to be resource, array given in C:"
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
解决方案 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.
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();
Then change the loop to:
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>";
}
The Joomla database object should be used for all database work, don't try to use any of the mysql_*
functions.
相关文章