Back to Top

How to use Custom Query in Joomla?

Custom Query with Joomla

Joomla is an open source content Management System.Sometimes, we are working on some customization in an existing functionality and database.so for that it is necessary to write custom queries into Joomla.

Joomla provides JDatabase class to execute joomla database. joomla 2.5 has introduced the JDatabaseQuery class concept.

Let’s understand the custom queries.Here I am going to start with select query

SELECT a records

Here,getDBO is for the connection to the global database which creates global database object if not exists.setQuery the method sets the SQL query for later execution.For listing fields, I have used loadObjectList method which returns the list of fields into Object form but Joomla has various methods to fetch records and it’s depending on what format of data you want. so Let’s take a look into it.

1. loadResult

returns the first column from a row or a single specified column similar to the MySQL function “mysql_fetch_row()” if you specify a single column.

2. loadResultArray

returns an array of single field results into an array.

3. loadAssoc

returns an associative array of the first row.

4. loadAssocList

returns an array of the result set rows in an associative array

5. loadRow

returns the first row returned by the query in a numeric array.

6. loadRowList

returns a list of database rows in a numeric array.

7. loadObject

returns the first row of a query into an object.

8. loadObjectList

returns a list of database objects.

9. loadNextObject

returns the next row returned by the query.

8. loadNextRow

returns the next row returned by the query.

INSERT a record

After fetching rows from the database, let’s see how to insert records into the database.

To get the last inserted id,you can use following code:

UPDATE a record

Joomla provides methods for building update queries.update method generates exception if a record does not exist or fail to update.

DELETE a record

As like Joomla have selected, insert and update method calls, there is also a delete method for deleting records from the database.

COMMON query

This is for executing any query, even for join and other complex query, you can manage simply using below line of code.

As always thank you for reading and feel free to share your thoughts! Don’t Forget to Follow us on Twitter or Subscribe us to Get the Latest Updates.

Comments (2)

  1. Hi, I would be curious whether SQL injection is an issue here or not?

    1. JDatabaseQuery class automatically filters the input so query is sql injection safe.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

How To Install Bugzilla on Windows

Posted on 12 years ago

Bhumi

Newly Approved Joomla Module

Posted on 11 years ago

Bhumi

How to use Flickr API with PHP

Posted on 12 years ago

Bhumi