[REVIEW] database->query

Last reviewed: Not reviewed


Executes the current SQL query string.

The syntax and semantics of SQL statements are beyond the scope of this manual.  Prior to Joomla 1.1 the only database software supported was MySQL and the reference manual may be found at http://dev.mysql.com/doc/. From Joomla 1.1 onwards support for multiple databases is provided using the ADODB database abstraction library.


object query ( [array $params] )

Array of parameters. This parameter is optional in Joomla 1.1 onwards and if omitted will default to an empty array. Introduced in Joomla 1.1 (ignored by prior versions).

This function returns a database resource object if successful or false if not.  The error code can be examined by calling database->getErrorNum and the error message can be obtained by calling database->getErrorMsg.



function countCategories() {
  global $database;
  $sql = "SELECT * FROM #__categories";
  $database->setQuery( $sql );
  if (!$result = $database->query()) {
    echo $database->stderr();
    return false;
  return $database->getNumRows( $result );
echo 'There are ' . countCategories() . ' categories';

might produce:

There are 6 categories

Please report any errors on this page to the Developer Documentation Forum.

Last Updated ( Monday, 25 December 2006 )