简体   繁体   English

joomla 2.5,如何从数据库中检索数据?

[英]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 为了从数据库中检索数据,我在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 和我的helloworld.php文件放入此代码

<?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 和tmpl / default.php文件代码是

<?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 $ db-> loadObjectList()加载对象数组,该对象数组不能作为字符串回显,请尝试

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

OR: 要么:

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

in helper.php after $results = $db->loadObjectList(); 在$ results = $ db-> loadObjectList()之后的helper.php中; 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 并在tmpl / default.php文件中执行foreach循环

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.%'));

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

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