Joomla Last Database Query!
Joomla is a popular CMS used in creating websites easily without need to know the web development languages such as html or php. However, if you are a web developer who develops custom modules or components for Joomla, you will definitely have to face situations where you need to print the last executed mysql database query for debugging purposes. The scope of this article is to show the way of printing the last query.
As I have already mentioned, the following method is very useful when debugging your database related bugs in your Joomla component or module. Let us assume that you are running a simple query as below;
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('*');
$query->from($db->quoteName('my_users'));
$query->order('user_id ASC');
$db->setQuery($query);
What I have done here is simply selecting all the data from my “my_users” table. I believe you already know what you have to do to get the data and print it from the above query. Let's say if you did not get the expected results from the above query, and you are sure that the table already has got data in it. Then definitely there should be something wrong with the query. So let's print it and see whether our query is properly generated. Adding the following lines below the above code will print the last query which is being executed.
echo $db->getQuery();
die(); //break the code below this line.
Now you can see the query in plain text, so you can check if there is anything wrong in it like incorrectly spelled table name, column name or any other mistake in your query. Also you can run this query in your phpmyadmin / or your mysql client software and see if it returns any data.