简体   繁体   中英

joomla 2.5, how to retrive data from database?

i want to show data from database on my module front page. To retrive data from database i write this code in helper.php

public static function getdb($params)
{
// Get a db connection.
$db = JFactory::getDbo();

// Create a new query object.
$query = $db->getQuery(true);

// Select all records from the user profile table where key begins with "custom.".
// Order it by the ordering field.
$query->select($db->quoteName(array('user_id', 'profile_key', 'profile_value',   'ordering')));
$query->from($db->quoteName('#__user_profiles'));
$query->where($db->quoteName('profile_key') . ' LIKE '. $db->quote('\'custom.%\''));
$query->order('ordering ASC');

// Reset the query using our newly populated query object.
$db->setQuery($query);

// Load the results as a list of stdClass objects (see later for more options on    retrieving data).
$results = $db->loadObjectList();
foreach($results as $value)
{
echo $value;
}
}

and my helloworld.php file put this code

<?php


// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

// Include the syndicate functions only once
require_once( dirname(__FILE__).DS.'helper.php' );


$hello = modHelloWorldHelper::getdb( $params );


require( JModuleHelper::getLayoutPath( 'mod_helloworld' ) );
?>

and tmpl/default.php file code is

<?php // no direct access
defined( '_JEXEC' ) or die( 'Restricted access' ); ?>
<?php echo $hello; ?>

but results is blank. nothing show in module page. how to i get data from database? how is the right format to get data from database?

$db->loadObjectList() loads an array of objects, which cannot be echo'ed as a string, try to

foreach($results as $value)
{
  var_dump($value);
}

OR:

foreach($results as $value)
{
  echo $value->user_id;
}

in helper.php after $results = $db->loadObjectList(); delete other code and insert this code( you should return value do NOT echo it )

return $results;

and in tmpl/default.php file do foreach loop

foreach($hello as $value)
{
  echo $value['user_id'];
}
SELECT `user_id`,`profile_key`,`profile_value`,`ordering`
FROM `pwsha_user_profiles`
WHERE `profile_key` LIKE '\'custom.%\''
ORDER BY ordering ASC

Is the generated query from your code.

I'm not sure what query you are trying to run and whether you actually meant this to be the query but I'm betting not.

I'd suggest

$query->where($db->quoteName('profile_key') . ' LIKE '. $db->quote('custom.%'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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