繁体   English   中英

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

[英]joomla 2.5, how to retrive data from database?

我想在模块首页上显示数据库中的数据。 为了从数据库中检索数据,我在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;
}
}

和我的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' ) );
?>

和tmpl / default.php文件代码是

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

但结果为空白。 模块页面无任何显示。 我如何从数据库中获取数据? 如何从数据库中获取数据的正确格式?

$ db-> loadObjectList()加载对象数组,该对象数组不能作为字符串回显,请尝试

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

要么:

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

在$ results = $ db-> loadObjectList()之后的helper.php中; 删除其他代码并插入此代码(您应该返回值,不要回显它)

return $results;

并在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

是从您的代码生成的查询。

我不确定您要尝试运行哪个查询,以及您实际上是否打算将其作为查询,但我不确定。

我建议

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

暂无
暂无

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

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