doupeng2253 2014-09-09 17:49
浏览 61

如何使用本机joomla 2.5 php对象显示Joomla数据库表

I am trying to display data from a MySQL database table using the native joomla PHP objects, however I can never get it to work right. It never displays any data.

I know how to display the data by connecting to the database the old fashion way, like below...

$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'];

However i'd like to display content like I have above, but by using the joomla native php objects, so iv'e created a sample php table with the following fields.....

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

and tried to display it (ALL ROWS) / OR (ONE ROW) using the following joomla code.....but nothing happens....no matter how I try to switch it up or change out parts of the code....So my thinking is that i'm missing a fundamental part of how this should work....Thanks again for your help! Also please do not quote joomla 2.5 component tutorial from joomla.org...that is where I've gotten this code and IMO it lacks a lot of information.

<?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;

?>
  • 写回答

1条回答 默认 最新

  • dpxpz37157 2014-09-09 18:15
    关注

    You have to correct: $query->from($db->quoteName('111testnames'));

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

    using #__ automatically adds the suffix of your table.

    Another error that I could see is the where clause doesn't have have an operator. Please try to correct it to $query->where($db->quoteName('Last') ." = ". $db->quote('buffet'));.

    In order to have a proper view of the results you have to use print_r($results); instead of echo $results; as there is an object array you have to display not a string.

    Good Luck!

    评论

报告相同问题?

悬赏问题

  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退