简体   繁体   English

在 Joomla 框架中显示 MySQL 选择查询结果

[英]Display MySQL select query results in Joomla framework

I'm trying to fetch query results from Joomla MySQL database in a Joomla page.我正在尝试从 Joomla 页面中的 Joomla MySQL 数据库中获取查询结果。 The print_r is returning proper results which the db connection is proper. print_r正在返回正确的结果,数据库连接是正确的。 But I'm not able to display the data that has been fetched.但是我无法显示已获取的数据。 Here's the result that I'm getting at the moment for print_r:这是我目前为 print_r 得到的结果:

mysqli_result Object ( 
    [current_field] => 0 
    [field_count] => 2 
    [lengths] => [num_rows] => 2 [type] => 0 
) 

Here's the code that I'm using:这是我正在使用的代码:

{source}
<script language="javascript" type="text/javascript">

</script>
<?php


    // init Joomla Framework
    define( '_JEXEC', 1 );
    define( 'DS', DIRECTORY_SEPARATOR );
    define( 'JPATH_BASE', realpath(dirname(__FILE__).DS.'..' ));


    require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
    require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );

    $mainframe = JFactory::getApplication('site');

    // DBQuery
    $database =& JFactory::getDBO();
    $query = "SELECT city_id, city_name FROM indytoad_city;";
    $database->setQuery($query);
    $result = $database->query();

    print_r($result);
?>
{/source}

Please let me know how and where and what I need to amend in order to display the query results.请让我知道我需要修改的方式、位置和内容,以显示查询结果。

Use loadAssocList() instead of query() , ie使用loadAssocList()而不是query() ,即

$result = $database->loadAssocList();

It gets the data from the database as an associative array.它从数据库中获取数据作为关联数组。

Joomla documentation: http://docs.joomla.org/JDatabase::loadAssocList/11.1 Joomla 文档: http : //docs.joomla.org/JDatabase :: loadAssocList/11.1

I dont want to go into details of convention used in Joomla to retrive result from database.我不想详细介绍 Joomla 中用于从数据库检索结果的约定。 This entirely depends on the developer.这完全取决于开发人员。

The reason you dont get the value because you probably treat the result array as an associative array.之所以没有得到值,是因为您可能将结果数组视为关联数组。 It is actually an stdClass object !它实际上是一个 stdClass 对象!

use

$result->city_id

to get the values of city_id, if in case of multiple nested classes are there, you can use it as获取city_id的值,如果存在多个嵌套类,则可以将其用作

$result->parent->child

Hope this helps !希望这可以帮助 !

for more info you can check this one out -> https://stackoverflow.com/a/931419/122840有关更多信息,您可以查看此信息-> https://stackoverflow.com/a/931419/122840

Also instead of也代替

$result = $database->query();

use :用 :

$result = $database->loadAssocList();

This loads the result in Associated array!这会将结果加载到关联数组中!

so you use所以你用

$result['column'] $result['列']

to get the result !得到结果!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM