如何使用原生joomla2.5php对象显示Joomla数据库表


How to Display Joomla database table using native joomla 2.5 php objects

我正试图使用原生的joomlaPHP对象显示MySQL数据库表中的数据,但我永远无法正常工作。它从不显示任何数据。

我知道如何通过旧的方式连接到数据库来显示数据,如下所示。。。

$con = mysqli_connect("localhost","xxxxxx_user10","$dbpass","xxxxxxx_final");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
} else {
//What is the base model
$sku = $finish;
// Select The Correct Table
$result = mysqli_query($con,"SELECT * FROM BAH_finish WHERE color_value='$sku' GROUP BY uniqueID ORDER BY uniqueID ASC");
  $row = mysqli_fetch_array($result);
  $finishname = $row['color_name'];
  $finishtype = $row['color_type'];

然而,我想像上面一样显示内容,但通过使用joomla原生php对象,所以iv'e创建了一个带有以下字段的示例php表。。。。。

| ID | Last | First | Address | City | State | ZIP

并尝试使用以下joomla代码显示它(所有行)/或(一行)。。。。。但什么也没发生。。。。无论我如何切换它或更改代码的某些部分。。。。所以我的想法是,我错过了这应该如何运作的一个基本部分。。。。再次感谢您的帮助!此外,请不要引用joomla.org上的joomla2.5组件教程……这就是我得到这段代码的地方,IMO缺乏很多信息。

<?php
// 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('Last', 'First', 'Address', 'City', 'State', 'ZIP')));
$query->from($db->quoteName('111testnames'));
$query->where($db->quoteName('Last') . $db->quote('buffet'));
// 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();
echo $results;
?>

您必须更正:$query->from($db->quoteName('111testnames'));

$query->from($db->quoteName('#__111testnames'));

使用#__会自动添加表的后缀。

我可以看到的另一个错误是where子句没有运算符。请尝试将其更正为$query->where($db->quoteName('Last') ." = ". $db->quote('buffet'));

为了正确查看结果,您必须使用print_r($results);而不是echo $results;,因为有一个对象数组,您必须显示而不是字符串。

祝你好运!