Many modules in Joomla require using a database. It is assumed in this tutorial that you already understand the basics of using the JDatabase class. If you don't please read the documentation on accessing the database using JDatabase before continuing this tutorial

Creating a table on install

To create the xml table on install we are going to add the following lines into mod_helloworld.xml:

Finally we'll just leave a placeholder in the updates file. There is an SQL file for each component version. Each file name must match the version string in the manifest file for that version. Joomla uses this string to determine which SQL files(s) to execute, and in what order they will be executed.

Important Note: These files are also used to set the version number in the #__schemas table. This version number must be present in the current version of the component in order for the new SQL files to be run during the update. For example, if you have version 1.0 and are updating to version 1.1, the 1.1.sql file will not be executed if there was no 1.0.sql file in the 1.0 release. For this reason, it is good practice to have a SQL update file for each version, even if there is no SQL change in that version.

# Placeholder file for database changes for version 1.0.0

Making the request in the helper file

Now on installing our module we should find that there is a helloworld database set up in our database schema with our hello's in. We must now retrieve this from the database to display to the user. We will now amend the getHello function we placed in the helper file in the last part.

For now we'll ignore using form fields to choose a hello and just retrieve the English shout

//Obtain a database connection$db= JFactory::getDbo();//Retrieve the shout$query=$db->getQuery(true)->select($db->quoteName('hello'))->from($db->quoteName('#__helloworld'))->where('lang = '.$db->Quote('en-GB'));//Prepare the query$db->setQuery($query);// Load the row.$result=$db->loadResult();//Return the Helloreturn$result;

Conclusion

Using modules with database connections for Joomla! is a fairly simple, straightforward process. Using the techniques described in this tutorial, a lot of modules can be developed with little hassle, with updates easy to manage