如何打印出Joomla查询?


14

假设您已经使用Joomla构建了一个查询。

// 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();

来自此处的示例:https : //docs.joomla.org/Selecting_data_using_JDatabase

是否有命令打印查询语句(不是结果而是实际的SQL)?


这样我就可以将查询读取为“ SELECT * FROM ....”
Mat Kay

1
为什么不使用调试模式?
jdog

Answers:



9

您还可以使用Joomla replacePrefix函数,该函数以可以直接插入PhpMyAdmin之类的格式转储查询。

这是一个例子:

$db = JFactory::getDbo();

$query = $db->getQuery(true);
$query->select($db->quoteName('something'))
      ->from($db->quoteName('#__content')); 
$db->setQuery($query);

// Dump the query
echo $db->replacePrefix((string) $query);

将输出以下内容:

SELECT `something` FROM `jos_content`


4

JDatabaseQuery对象具有__toString()输出查询的功能,因此您可以执行以下操作:

echo $db->getQuery();

或者,如果要将其传递给函数,则可以先将其显式转换为字符串:

var_dump((string)$db->getQuery());


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.