<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://docs.sandbox.joomla.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Haydenyoung</id>
	<title>Joomla! Documentation - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://docs.sandbox.joomla.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Haydenyoung"/>
	<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/Special:Contributions/Haydenyoung"/>
	<updated>2026-10-16T04:01:48Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.43.0</generator>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Subform_form_field_type&amp;diff=419684</id>
		<title>Subform form field type</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Subform_form_field_type&amp;diff=419684"/>
		<updated>2017-06-10T11:54:01Z</updated>

		<summary type="html">&lt;p&gt;Haydenyoung: Some language syntax and spelling corrections&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;noinclude&amp;gt;&amp;lt;languages /&amp;gt;&amp;lt;/noinclude&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:1--&amp;gt;&lt;br /&gt;
The &#039;&#039;&#039;subform&#039;&#039;&#039; form field type provides a method for using XML forms inside one another or reuse forms inside an existing  form.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:2--&amp;gt;&lt;br /&gt;
If attribute &#039;&#039;&#039;multiple&#039;&#039;&#039; is set to &#039;&#039;&#039;true&#039;&#039;&#039; then the included form will be &#039;&#039;&#039;repeatable&#039;&#039;&#039;.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:3--&amp;gt;&lt;br /&gt;
The Field has two &amp;quot;predefined&amp;quot; layouts for displaying the subform as either a &#039;&#039;&#039;table&#039;&#039;&#039; or as a &#039;&#039;&#039;div&#039;&#039;&#039; container, as well as support for custom layouts.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:5--&amp;gt;&lt;br /&gt;
An example XML &#039;&#039;&#039;field&#039;&#039;&#039; definition for &#039;&#039;&#039;single&#039;&#039;&#039; mode:&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&amp;lt;field name=&amp;quot;field-name&amp;quot; type=&amp;quot;subform&amp;quot;&lt;br /&gt;
    formsource=&amp;quot;path/to/exampleform.xml&amp;quot;&lt;br /&gt;
    label=&amp;quot;Subform Field&amp;quot; description=&amp;quot;Subform Field Description&amp;quot; /&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:6--&amp;gt;&lt;br /&gt;
An example XML &#039;&#039;&#039;field&#039;&#039;&#039; definition for &#039;&#039;&#039;multiple&#039;&#039;&#039; mode:&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&amp;lt;field name=&amp;quot;field-name&amp;quot; type=&amp;quot;subform&amp;quot;&lt;br /&gt;
    formsource=&amp;quot;path/to/exampleform.xml&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
    label=&amp;quot;Subform Field&amp;quot; description=&amp;quot;Subform Field Description&amp;quot; /&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:7--&amp;gt;&lt;br /&gt;
Example XML of &#039;&#039;exampleform.xml&#039;&#039;&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;form&amp;gt;&lt;br /&gt;
    &amp;lt;field name=&amp;quot;example_text&amp;quot; type=&amp;quot;text&amp;quot; label=&amp;quot;Example Text&amp;quot; /&amp;gt;&lt;br /&gt;
    &amp;lt;field name=&amp;quot;example_textarea&amp;quot; type=&amp;quot;textarea&amp;quot; label=&amp;quot;Example Textarea&amp;quot; cols=&amp;quot;40&amp;quot; rows=&amp;quot;8&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:8--&amp;gt;&lt;br /&gt;
An example XML of &#039;&#039;exampleform.xml&#039;&#039; with &#039;&#039;fieldsets&#039;&#039;&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;form&amp;gt;&lt;br /&gt;
    &amp;lt;fieldset name=&amp;quot;section1&amp;quot; label=&amp;quot;Section1&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;field name=&amp;quot;example_text&amp;quot; type=&amp;quot;text&amp;quot; label=&amp;quot;Example Text&amp;quot; /&amp;gt;&lt;br /&gt;
        &amp;lt;field name=&amp;quot;example_textarea&amp;quot; type=&amp;quot;textarea&amp;quot; label=&amp;quot;Example Textarea&amp;quot; cols=&amp;quot;40&amp;quot; rows=&amp;quot;8&amp;quot; /&amp;gt;&lt;br /&gt;
    &amp;lt;/fieldset&amp;gt;&lt;br /&gt;
    &amp;lt;fieldset name=&amp;quot;section2&amp;quot; label=&amp;quot;Section2&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;field name=&amp;quot;example_list&amp;quot; type=&amp;quot;list&amp;quot; default=&amp;quot;1&amp;quot; class=&amp;quot;advancedSelect&amp;quot; label=&amp;quot;Example List&amp;quot;&amp;gt;&lt;br /&gt;
            &amp;lt;option value=&amp;quot;1&amp;quot;&amp;gt;JYES&amp;lt;/option&amp;gt;&lt;br /&gt;
            &amp;lt;option value=&amp;quot;0&amp;quot;&amp;gt;JNO&amp;lt;/option&amp;gt;&lt;br /&gt;
        &amp;lt;/field&amp;gt;&lt;br /&gt;
    &amp;lt;/fieldset&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:9--&amp;gt;&lt;br /&gt;
Field attributes:&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:10--&amp;gt;&lt;br /&gt;
* &#039;&#039;&#039;type&#039;&#039;&#039; (mandatory) must be &#039;&#039;subform&#039;&#039;.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:11--&amp;gt;&lt;br /&gt;
* &#039;&#039;&#039;name&#039;&#039;&#039; (mandatory) is the unique name of the field.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:12--&amp;gt;&lt;br /&gt;
* &#039;&#039;&#039;label&#039;&#039;&#039; (mandatory) (translatable) is the descriptive title of the field.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:13--&amp;gt;&lt;br /&gt;
* &#039;&#039;&#039;description&#039;&#039;&#039; (optional) (translatable) is text that will be shown as a tooltip when the user moves the mouse over the drop-down box.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:14--&amp;gt;&lt;br /&gt;
* &#039;&#039;&#039;required&#039;&#039;&#039; (optional) The field must be filled before submitting the form.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:15--&amp;gt;&lt;br /&gt;
* &#039;&#039;&#039;message&#039;&#039;&#039; (optional) The error message that will be displayed instead of the default message.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:16--&amp;gt;&lt;br /&gt;
* &#039;&#039;&#039;default&#039;&#039;&#039; (optional) is the default value, JSON string.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:17--&amp;gt;&lt;br /&gt;
* &#039;&#039;&#039;formsource&#039;&#039;&#039; (mandatory) the form source to be included. A relative path to the xml file or a valid form name which can be found by &#039;&#039;JForm::getInstance()&#039;&#039;.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:18--&amp;gt;&lt;br /&gt;
* &#039;&#039;&#039;multiple&#039;&#039;&#039; (optional) whether the subform fields are repeatable or not.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:19--&amp;gt;&lt;br /&gt;
* &#039;&#039;&#039;min&#039;&#039;&#039; (optional) count of minimum repeating in multiple mode. Default: &#039;&#039;0&#039;&#039;.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:20--&amp;gt;&lt;br /&gt;
* &#039;&#039;&#039;max&#039;&#039;&#039; (optional) count of maximum repeating in multiple mode. Default: &#039;&#039;1000&#039;&#039;.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:21--&amp;gt;&lt;br /&gt;
* &#039;&#039;&#039;groupByFieldset&#039;&#039;&#039; (optional) whether to group the subform fields by its fieldset (&#039;&#039;true&#039;&#039; or &#039;&#039;false&#039;&#039;). Default: &#039;&#039;false&#039;&#039;.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:22--&amp;gt;&lt;br /&gt;
* &#039;&#039;&#039;buttons&#039;&#039;&#039; (optional) which buttons to show in multiple mode. Default: &#039;&#039;add,remove,move&#039;&#039;.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:23--&amp;gt;&lt;br /&gt;
* &#039;&#039;&#039;layout&#039;&#039;&#039; (optional) the name of the layout to use when displaying &#039;&#039;&#039;subform&#039;&#039;&#039; fields.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:24--&amp;gt;&lt;br /&gt;
Available layouts:&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:25--&amp;gt;&lt;br /&gt;
* &#039;&#039;&#039;joomla.form.field.subform.default&#039;&#039;&#039; render the subform in a div container, without support of repeating. Default for single mode.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:26--&amp;gt;&lt;br /&gt;
* &#039;&#039;&#039;joomla.form.field.subform.repeatable&#039;&#039;&#039; render the subform in a div container, used for multiple mode. Support groupByFieldset.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:27--&amp;gt;&lt;br /&gt;
* &#039;&#039;&#039;joomla.form.field.subform.repeatable-table&#039;&#039;&#039; render the subform as a table, used for multiple mode. Supports groupByFieldset. By default each field is rendered as a table column, but if &#039;&#039;groupByFieldset=true&#039;&#039; then each fieldset is rendered as a table column.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:28--&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Be aware&#039;&#039;&#039;&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:29--&amp;gt;&lt;br /&gt;
If your field in the subform has additional JavaScript logic then it may not work in &#039;&#039;&#039;multiple&#039;&#039;&#039; mode, because do not see the fields which added by the subform field dynamically. If it happened then you need to adjust your field to support it.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:30--&amp;gt;&lt;br /&gt;
Next example may help:&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;javascript&amp;quot;&amp;gt;jQuery(document).ready(function(){&lt;br /&gt;
    ... here the code for setup your field as usual...&lt;br /&gt;
&lt;br /&gt;
    jQuery(document).on(&#039;subform-row-add&#039;, function(event, row){&lt;br /&gt;
        ... here is the code to set up the fields in the new row ...&lt;br /&gt;
    })&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:31--&amp;gt;&lt;br /&gt;
Because of this some extra Joomla! fields may not work for now.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--T:35--&amp;gt;&lt;br /&gt;
Subform not provide the Validation and Filter.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
== Example == &amp;lt;!--T:36--&amp;gt;&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:37--&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Problem &#039;&#039;&#039;&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:38--&amp;gt;&lt;br /&gt;
After adding new rows selects are not &amp;quot;chosen&amp;quot;.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:39--&amp;gt;&lt;br /&gt;
See screenshot to get the idea of the problem http://static.xscreenshot.com/2016/10/29/18/screen_6917fc3c8d3c5d679b6442ff249a7b5f&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:40--&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Solution &#039;&#039;&#039;&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:41--&amp;gt;&lt;br /&gt;
Here is an example how to reinit jQuery Chosen on newly added repeated rows:&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;javascript&amp;quot;&amp;gt;jQuery(document).ready(function(){&lt;br /&gt;
    jQuery(document).on(&#039;subform-row-add&#039;, function(event, row){&lt;br /&gt;
        jQuery(row).find(&#039;select&#039;).chosen();&lt;br /&gt;
    })&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:42--&amp;gt;&lt;br /&gt;
Or a PHP snippet to be used in e.g. your plugin in **onBeforeCompileHead** method or in your component view.&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$doc = JFactory::getDocument();&lt;br /&gt;
$js = &#039;&lt;br /&gt;
	jQuery(document).on(\&#039;subform-row-add\&#039;, function(event, row){&lt;br /&gt;
		jQuery(row).find(\&#039;select\&#039;).chosen();&lt;br /&gt;
	})&lt;br /&gt;
&#039;;&lt;br /&gt;
$doc-&amp;gt;addScriptDeclaration($js);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&amp;lt;!--T:43--&amp;gt;&lt;br /&gt;
So newly added rows now are &amp;quot;chosen&amp;quot; now http://static.xscreenshot.com/2016/10/29/18/screen_8f7dc7709899bac7a43926828e504519&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
=== See also === &amp;lt;!--T:32--&amp;gt;&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:33--&amp;gt;&lt;br /&gt;
* [[S:MyLanguage/Standard form field types|List of standard form field types]]&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;noinclude&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:34--&amp;gt;&lt;br /&gt;
[[Category:Standard form field types]]&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&amp;lt;/noinclude&amp;gt;&lt;/div&gt;</summary>
		<author><name>Haydenyoung</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Package&amp;diff=219518</id>
		<title>Package</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Package&amp;diff=219518"/>
		<updated>2015-08-21T20:36:34Z</updated>

		<summary type="html">&lt;p&gt;Haydenyoung: Fix spelling.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;noinclude&amp;gt;&amp;lt;languages /&amp;gt;&amp;lt;/noinclude&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
== Note on component dependent modules== &amp;lt;!--T:1--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--T:2--&amp;gt;&lt;br /&gt;
Sometimes modules (or plugins etc.) use a component&#039;s models and helpers and such. In these occasions it would be nice to be able to package your module in this way that it gets uninstalled when the component itself gets uninstalled.  This is called dependency management which has been desired in joomla but hasn&#039;t been done yet.&lt;br /&gt;
It is suggested to follow the standards and just using a package as is described below.&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
== What is a package? == &amp;lt;!--T:3--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--T:4--&amp;gt;&lt;br /&gt;
A package is a extension that is used to install multiple extensions in one go. This is used if you have for example a component and a module that are depending on each other. Combining them in a package will let the user install and uninstall both extensions in one go. Package extensions can be used with Joomla {{JVer|2.5}} and higher.&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
== How do I create a package? == &amp;lt;!--T:5--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--T:6--&amp;gt;&lt;br /&gt;
A package extension is created by zipping all zip files of the extensions together with a xml manifest file. For example if you have a package composed by:&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:7--&amp;gt;&lt;br /&gt;
* &#039;&#039;&#039;component&#039;&#039;&#039; helloworld&lt;br /&gt;
* &#039;&#039;&#039;module&#039;&#039;&#039; helloworld&lt;br /&gt;
* &#039;&#039;&#039;library&#039;&#039;&#039; helloworld &lt;br /&gt;
* &#039;&#039;&#039;system plugin&#039;&#039;&#039; helloworld &lt;br /&gt;
* &#039;&#039;&#039;template&#039;&#039;&#039; helloworld&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--T:8--&amp;gt;&lt;br /&gt;
The package should have the following tree in your zipfile:&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  -- pkg_helloworld.xml&lt;br /&gt;
  -- packages &amp;lt;dir&amp;gt;&lt;br /&gt;
      |-- com_helloworld.zip&lt;br /&gt;
      |-- mod_helloworld.zip&lt;br /&gt;
      |-- lib_helloworld.zip&lt;br /&gt;
      |-- plg_sys_helloworld.zip&lt;br /&gt;
      |-- tpl_helloworld.zip&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:9--&amp;gt;&lt;br /&gt;
The pkg_helloworld.xml could have the following contents:&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot; ?&amp;gt;&lt;br /&gt;
 &amp;lt;extension type=&amp;quot;package&amp;quot; version=&amp;quot;1.6&amp;quot; method=&amp;quot;upgrade&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;name&amp;gt;Hello World Package&amp;lt;/name&amp;gt;&lt;br /&gt;
 &amp;lt;author&amp;gt;Hello World Package Team&amp;lt;/author&amp;gt;&lt;br /&gt;
 &amp;lt;creationDate&amp;gt;May 2012&amp;lt;/creationDate&amp;gt;&lt;br /&gt;
 &amp;lt;packagename&amp;gt;helloworld&amp;lt;/packagename&amp;gt;&lt;br /&gt;
 &amp;lt;version&amp;gt;1.0.0&amp;lt;/version&amp;gt;&lt;br /&gt;
 &amp;lt;url&amp;gt;http://www.yoururl.com/&amp;lt;/url&amp;gt;&lt;br /&gt;
 &amp;lt;packager&amp;gt;Hello World Package Team&amp;lt;/packager&amp;gt;&lt;br /&gt;
 &amp;lt;packagerurl&amp;gt;http://www.yoururl.com/&amp;lt;/packagerurl&amp;gt;&lt;br /&gt;
 &amp;lt;description&amp;gt;Example package to combine multiple extensions&amp;lt;/description&amp;gt;&lt;br /&gt;
 &amp;lt;update&amp;gt;http://www.updateurl.com/update&amp;lt;/update&amp;gt;&lt;br /&gt;
 &amp;lt;files folder=&amp;quot;packages&amp;quot;&amp;gt;&lt;br /&gt;
   &amp;lt;file type=&amp;quot;component&amp;quot; id=&amp;quot;com_helloworld&amp;quot; &amp;gt;com_helloworld.zip&amp;lt;/file&amp;gt;&lt;br /&gt;
   &amp;lt;file type=&amp;quot;module&amp;quot; id=&amp;quot;helloworld&amp;quot; client=&amp;quot;site&amp;quot;&amp;gt;mod_helloworld.zip&amp;lt;/file&amp;gt;&lt;br /&gt;
   &amp;lt;file type=&amp;quot;library&amp;quot; id=&amp;quot;helloworld&amp;quot;&amp;gt;lib_helloworld.zip&amp;lt;/file&amp;gt;&lt;br /&gt;
   &amp;lt;file type=&amp;quot;plugin&amp;quot; id=&amp;quot;helloworld&amp;quot; group=&amp;quot;system&amp;quot;&amp;gt;plg_sys_helloworld.zip&amp;lt;/file&amp;gt;&lt;br /&gt;
   &amp;lt;file type=&amp;quot;template&amp;quot; id=&amp;quot;helloworld&amp;quot; client=&amp;quot;site&amp;quot;&amp;gt;tpl_helloworld.zip&amp;lt;/file&amp;gt;&lt;br /&gt;
 &amp;lt;/files&amp;gt;&lt;br /&gt;
 &amp;lt;/extension&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:10--&amp;gt;&lt;br /&gt;
When you zip this and try to install it, you will see that every extension will be installed. Also the package will be visible in the extension list to uninstall all the extension of this package.&lt;br /&gt;
&amp;lt;/translate&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:11--&amp;gt;&lt;br /&gt;
Remember to use the package uninstaller instead of individual subpackage uninstallers to avoid orphan extension entries in the extension manager.&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
=== Id= &amp;lt;file id=&amp;quot;not_arbitrary&amp;quot;&amp;gt; === &amp;lt;!--T:12--&amp;gt;&lt;br /&gt;
The id element in &amp;lt;file ..&amp;gt; tag are NOT arbitrary!. The &amp;quot;id=&amp;quot; should be set to the value of the &amp;quot;element&amp;quot; column in the &amp;quot;#__extensions&amp;quot; table. If they are not set correctly, upon uninstalling of the package the children ( &amp;lt;file&amp;gt; ) will NOT be found and uninstalled.&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
=== Manifest filename and packagename === &amp;lt;!--T:13--&amp;gt;&lt;br /&gt;
The naming of the manifest file and the ability to uninstall the package file itself are closely related. &lt;br /&gt;
The manifest file must have a &amp;quot;pkg_&amp;quot; prefix, the remainder of the manifest name ( without .xml extension ) is to be used as &amp;lt;packagename&amp;gt;. Or, the other way around, a package you want to identify as &amp;quot;blurpblurp_J3&amp;quot; get that as its &amp;lt;packagename&amp;gt; and should be in a manifest file named &amp;quot;pkg_blurpblurp_J3.xml&amp;quot;. Failure to do so will make it impossible to uninstall the package itself.&lt;br /&gt;
&lt;br /&gt;
=== Extension tag === &amp;lt;!--T:15--&amp;gt;&lt;br /&gt;
As of Joomla 3.4, you should include method=&amp;quot;upgrade&amp;quot; in the &amp;lt;extension&amp;gt; tag if you wish for your package to succeed in updating itself on subsequent installations.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--T:16--&amp;gt;&lt;br /&gt;
The version attribute of the &amp;lt;extension&amp;gt; tag should be set to the minimum supported version of Joomla for this package. For example, if your package only supports Joomla 3.2 and above set this to 3.2.&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:14--&amp;gt;&lt;br /&gt;
[[Category:Extensions]]&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;/div&gt;</summary>
		<author><name>Haydenyoung</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Display_error_messages_and_notices&amp;diff=120544</id>
		<title>Display error messages and notices</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Display_error_messages_and_notices&amp;diff=120544"/>
		<updated>2014-06-20T12:36:31Z</updated>

		<summary type="html">&lt;p&gt;Haydenyoung: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{RightTOC}}&lt;br /&gt;
Errors, warnings and notices can be displayed from any component, module, plugin or template using the methods outlined below.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a handle to the Joomla! application object&lt;br /&gt;
$application = JFactory::getApplication();&lt;br /&gt;
&lt;br /&gt;
// Add a message to the message queue&lt;br /&gt;
$application-&amp;gt;enqueueMessage(JText::_(&#039;SOME_ERROR_OCCURRED&#039;), &#039;error&#039;);&lt;br /&gt;
&lt;br /&gt;
/** Alternatively you may use chaining */&lt;br /&gt;
JFactory::getApplication()-&amp;gt;enqueueMessage(JText::_(&#039;SOME_ERROR_OCCURRED&#039;), &#039;error&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The second argument to the [http://api.joomla.org/11.4/Joomla-Platform/Application/JApplication.html#enqueueMessage enqueueMessage] function is the type of the message. The default is &#039;message&#039;, but &#039;error&#039; results in a different style for the message.  The message will be displayed in place of a special &amp;lt;tt&amp;gt;jdoc:include&amp;lt;/tt&amp;gt; statement in your template.  Place the following in your template at the location where you want messages to appear.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;jdoc:include type=&amp;quot;message&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Message ==&lt;br /&gt;
&amp;lt;div style=&amp;quot;background-color: #c3d2e5; color: #0055bb; border-top: 3px solid #84a7db; border-bottom: 3px solid #84a7db; padding-left: 1em; font-weight: bold;&amp;quot;&amp;gt;&lt;br /&gt;
Message&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
JFactory::getApplication()-&amp;gt;enqueueMessage(&#039;Message&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Notice ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;background-color: #efe7b8; color: #c00; border-top: 3px solid #f0dc7e; border-bottom: 3px solid #f0dc7e; padding-left: 1em; font-weight: bold;&amp;quot;&amp;gt;&lt;br /&gt;
Notice&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
JError::raiseNotice( 100, &#039;Notice&#039; );&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Warning ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;background-color: #e6c0c0; color: #c00; border-top: 3px solid #DE7A7B; border-bottom: 3px solid #DE7A7B; padding-left: 1em; font-weight: bold;&amp;quot;&amp;gt;&lt;br /&gt;
Warning&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
JError::raiseWarning( 100, &#039;Warning&#039; );&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Error ==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
JError::raiseError( 4711, &#039;A severe error occurred&#039; );&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Joomla! 3.x is bootstrapped ==&lt;br /&gt;
Since Joomla! {{JVer|3.x}} uses bootstraped templates, the messages will use the standard bootstrap CSS styles for Alerts.&lt;br /&gt;
&lt;br /&gt;
See: http://getbootstrap.com/2.3.2/components.html#alerts&lt;br /&gt;
&lt;br /&gt;
The general syntax remains:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
JFactory::getApplication()-&amp;gt;enqueueMessage(&#039;Your Message&#039;, &#039;type&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Where &#039;&#039;&#039;type&#039;&#039;&#039; can be one of&lt;br /&gt;
&lt;br /&gt;
* &#039;warning&#039; - yellow&lt;br /&gt;
* &#039;notice&#039; - blue&lt;br /&gt;
* &#039;error&#039; - red&lt;br /&gt;
* &#039;message&#039; (or empty) - green&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [http://api.joomla.org/11.4/Joomla-Platform/Error/JError.html JError on api.joomla.org]&lt;br /&gt;
*  [http://api.joomla.org/11.4/Joomla-Platform/Application/JApplication.html JApplication on api.joomla.org]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;noinclude&amp;gt;[[Category:Development]]&amp;lt;/noinclude&amp;gt;&lt;br /&gt;
[[Category:Tutorials]][[Category:Component Development]]&lt;/div&gt;</summary>
		<author><name>Haydenyoung</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J2.5:Accessing_the_database_using_JDatabase&amp;diff=77657</id>
		<title>J2.5:Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J2.5:Accessing_the_database_using_JDatabase&amp;diff=77657"/>
		<updated>2012-11-20T17:08:08Z</updated>

		<summary type="html">&lt;p&gt;Haydenyoung: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{underconstruction}}&lt;br /&gt;
&lt;br /&gt;
Joomla provides a sophisticated database abstraction layer to simplify the usage for third party developers. New versions of the Joomla Platform API provide additional functionality which extends the database layer further, and includes features such as connectors to a greater variety of database servers and the query chaining to improve readability of connection code and simplify SQL coding.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
&lt;br /&gt;
Joomla can use different kinds of SQL database systems and run in a variety of environments with different table-prefixes. In addition to these functions, the class automatically creates the database connection. Besides instantiating the object you need just two lines of code to get a result from the database in a variety of formats. Using the Joomla database layer ensures a maximum of compatibility and flexibility for your extension.&lt;br /&gt;
&lt;br /&gt;
===The Connection===&lt;br /&gt;
&lt;br /&gt;
Joomla supports a generic interface for connecting to the database, hiding the intricacies of a specific database server from the Framework developer, thus making Joomla code easier to port from one platform to another.&lt;br /&gt;
&lt;br /&gt;
To obtain a database connection, use the [http://api.joomla.org/Joomla-Platform/JFactory.html#getDbo JFactory getDbo] static method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The getDbo method will return a reference to a high-level abstract class called [http://api.joomla.org/Joomla-Platform/Database/JDatabaseDriver.html JDatabaseDriver]; the Joomla Framework hides the specifics of the database server, which is loaded dynamically by getDbo via Joomla&#039;s configuration file.&lt;br /&gt;
&lt;br /&gt;
Alternatively, if your class inherits from the JModel (or a decendent class) getDbo method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = $this-&amp;gt;getDbo();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{JVer|3.0}} users should note that the JModel class has been renamed JModelLegacy and will eventually be deprecated in favour of the class JModelBase. Therefore, any Joomla 3.0 core or extension code should use JFactory::getDbo() to obtain a database connection.&lt;br /&gt;
&lt;br /&gt;
===The Query===&lt;br /&gt;
&lt;br /&gt;
Joomla&#039;s database querying has changed since the new Joomla Framework was introduced  &amp;quot;query chaining&amp;quot; is now the recommended method for building database queries (although string queries are still supported).&lt;br /&gt;
&lt;br /&gt;
Query chaining refers to a method of connecting a number of methods, one after the other, with each method returning an object that can support the next method, improving readability and simplifying code.&lt;br /&gt;
&lt;br /&gt;
To obtain a new instance of the JDatabaseQuery class we use the JDatabaseDriver getQuery method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The JDatabaseDriver::getQuery takes an optional argument, $new, which can be true or false (the default being false).&lt;br /&gt;
&lt;br /&gt;
To query our data source we can call a number of JDatabaseQuery methods; these methods encapsulate the data source&#039;s query language (in most cases SQL), hiding query-specific syntax from the developer and increasing the portability of the developer&#039;s source code. &lt;br /&gt;
&lt;br /&gt;
Some of the more frequently used methods include; select, from, join, where and order. There are also methods such as insert, update and delete for modifying records in the data store. By chaining these and other method calls, you can create almost any query against your data store without compromising portability of your code.&lt;br /&gt;
&lt;br /&gt;
====Selecting Records from a Single Table====&lt;br /&gt;
&lt;br /&gt;
Below is an example of creating a database query using the JDatabaseQuery class. Using the select, from, where and order methods, we can create queries which are flexible, easily readable and portable:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Select all records from the user profile table where key begins with &amp;quot;custom.&amp;quot;.&lt;br /&gt;
// Order it by the ordering field.&lt;br /&gt;
$query-&amp;gt;select(array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;));&lt;br /&gt;
$query-&amp;gt;from(&#039;#__user_profiles&#039;);&lt;br /&gt;
$query-&amp;gt;where(&#039;profile_key LIKE \&#039;custom.%\&#039;&#039;);&lt;br /&gt;
$query-&amp;gt;order(&#039;ordering ASC&#039;);&lt;br /&gt;
&lt;br /&gt;
// Reset the query using our newly populated query object.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
// Load the results as a list of stdClass objects.&lt;br /&gt;
$results = $db-&amp;gt;loadObjectList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The query can also be chained to simplify further:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;select(array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;))&lt;br /&gt;
    -&amp;gt;from(&#039;#__user_profiles&#039;)&lt;br /&gt;
    -&amp;gt;where(&#039;profile_key LIKE \&#039;custom.%\&#039;&#039;)&lt;br /&gt;
    -&amp;gt;order(&#039;ordering ASC&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Chaining can become useful when queries become longer and more complex.&lt;br /&gt;
&lt;br /&gt;
====Selecting Records from Multiple Tables====&lt;br /&gt;
&lt;br /&gt;
Using the JDatabaseQuery&#039;s [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#join join] methods, we can select records from multiple related tables. The generic &amp;quot;join&amp;quot; method takes two arguments; the join &amp;quot;type&amp;quot; (inner, outer, left, right) and the join condition. In the following example you will notice that we can use all of the keywords we would normally use if we were writing a native SQL query, including the AS keyword for aliasing tables and the ON keyword for creating relationships between tables. Also note that the table alias is used in all methods which reference table columns (I.e. select, where, order).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Select all articles for users who have a username which starts with &#039;a&#039;.&lt;br /&gt;
// Order it by the created date.&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;select(array(&#039;a.*&#039;, &#039;b.username&#039;, &#039;b.name&#039;))&lt;br /&gt;
    -&amp;gt;from(&#039;#__content AS a&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;INNER&#039;, &#039;#__users AS b ON (a.created_by = b.id)&#039;)&lt;br /&gt;
    -&amp;gt;where(&#039;b.username LIKE \&#039;a%\&#039;&#039;)&lt;br /&gt;
    -&amp;gt;order(&#039;a.created DESC&#039;);&lt;br /&gt;
&lt;br /&gt;
// Reset the query using our newly populated query object.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
// Load the results as a list of stdClass objects.&lt;br /&gt;
$results = $db-&amp;gt;loadObjectList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The join method above enables us to query both the content and user tables, retrieving articles with their author details. There are also convenience methods for [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#innerJoin inner], [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#leftJoin left], [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#rightJoin right] and [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#outerJoin outer] joins.&lt;br /&gt;
&lt;br /&gt;
We can use multiple joins to query across more than two tables:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;select(array(&#039;a.*&#039;, &#039;b.username&#039;, &#039;b.name&#039;, &#039;c.*&#039;, &#039;d.*&#039;))&lt;br /&gt;
    -&amp;gt;from(&#039;#__content AS a&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;INNER&#039;, &#039;#__users AS b ON (a.created_by = b.id)&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;LEFT&#039;, &#039;#__user_profiles AS c ON (b.id = c.user_id)&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;RIGHT&#039;, &#039;#__categories AS d ON (a.catid = d.id)&#039;)&lt;br /&gt;
    -&amp;gt;where(&#039;b.username LIKE \&#039;a%\&#039;&#039;)&lt;br /&gt;
    -&amp;gt;order(&#039;a.created DESC&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice how chaining makes the source code much more readable for these longer queries.&lt;br /&gt;
&lt;br /&gt;
====Inserting a Record====&lt;br /&gt;
&lt;br /&gt;
=====Using SQL=====&lt;br /&gt;
&lt;br /&gt;
The JDatabaseQuery class provides a number of methods for building insert queries, the most common being [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#insert insert], [http://api.joomla.org/11.4/Joomla-Platform/Database/JDatabaseQuery.html#columns columns] and [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#values values].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Insert columns.&lt;br /&gt;
$columns = array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;);&lt;br /&gt;
&lt;br /&gt;
// Insert values.&lt;br /&gt;
$values = array(1001, $db-&amp;gt;quote(&#039;custom.message&#039;), $db-&amp;gt;quote(&#039;Inserting a record using insert()&#039;), 1);&lt;br /&gt;
&lt;br /&gt;
// Prepare the insert query.&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;insert($db-&amp;gt;quoteName(&#039;#__user_profiles&#039;))&lt;br /&gt;
    -&amp;gt;columns($db-&amp;gt;quoteName($columns))&lt;br /&gt;
    -&amp;gt;values(implode(&#039;,&#039;, $values));&lt;br /&gt;
&lt;br /&gt;
// Reset the query using our newly populated query object.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since Joomla 3.0 there has been a change in how the Joomla database class executes a query.&lt;br /&gt;
&lt;br /&gt;
When executing a query in the Joomla Framework 11.4 we use the JDatabase&#039;s [http://api.joomla.org/11.4/Joomla-Platform/Database/JDatabase.html#query query] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
try {&lt;br /&gt;
    // Execute the query in Joomla 2.5.&lt;br /&gt;
    $result = $db-&amp;gt;query();&lt;br /&gt;
} catch (Exception $e) {&lt;br /&gt;
    // catch any database errors.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For Joomla Framework 12.1 and higher, we use the JDatabaseDriver&#039;s [http://api.joomla.org/Joomla-Platform/Database/JDatabaseDriver.html#execute execute] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
try {&lt;br /&gt;
    // Execute the query in Joomla 3.0.&lt;br /&gt;
    $result = $db-&amp;gt;execute();&lt;br /&gt;
} catch (Exception $e) {&lt;br /&gt;
    // catch any database errors.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since the phase out of PHP 4.x support, PHP&#039;s exception model has been widely implemented in the Joomla Framework. Therefore, we can wrap our database execution in a try...catch block to catch any unexpected database errors that may arise. Any database-specific errors will be thrown using an instance of the [http://api.joomla.org/Joomla-Platform/Database/JDatabaseException.html JDatabaseException] class although it is best practice to catch the generic Exception object from which JDatabaseException inherits.&lt;br /&gt;
&lt;br /&gt;
=====Using an Object=====&lt;br /&gt;
&lt;br /&gt;
The JDatabaseDriver also class provides us with a convenience method for saving an object directly to the database allowing us to add a record to a table without writing a single line of SQL.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Create and populate an object.&lt;br /&gt;
$profile = new stdClass();&lt;br /&gt;
$profile-&amp;gt;user_id = 1001;&lt;br /&gt;
$profile-&amp;gt;profile_key=&#039;custom.message&#039;;&lt;br /&gt;
$profile-&amp;gt;profile_value=&#039;Inserting a record using insertObject()&#039;;&lt;br /&gt;
$profile-&amp;gt;ordering=1;&lt;br /&gt;
&lt;br /&gt;
try {&lt;br /&gt;
    // Insert the object into the user profile table.&lt;br /&gt;
    $result = JFactory::getDbo()-&amp;gt;insertObject(&#039;#__user_profiles&#039;, $profile);&lt;br /&gt;
catch (Exception $e) {&lt;br /&gt;
    // catch any errors.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice here that we do not need to escape the table name; the insertObject method does this for us.&lt;br /&gt;
&lt;br /&gt;
The insertObject method will throw a [http://php.net/manual/en/class.runtimeexception.php RuntimeException] if there is a problem inserting the record into the database table.&lt;br /&gt;
&lt;br /&gt;
We need to ensure that the record does not exist before attempting to insert it, so adding some kind of record check before executing the insertObject method would be recommended.&lt;br /&gt;
&lt;br /&gt;
====Updating a Record====&lt;br /&gt;
&lt;br /&gt;
=====Using SQL=====&lt;br /&gt;
&lt;br /&gt;
The JDatabaseQuery class also provides methods for building update queries, in particular [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#update update] and [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#set set]. We also reuse another method which we used when creating select statements, the where method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Fields to update.&lt;br /&gt;
$fields = array(&lt;br /&gt;
    &#039;profile_value=\&#039;Updating custom message for user 1001.\&#039;&#039;,&lt;br /&gt;
    &#039;ordering=2&#039;);&lt;br /&gt;
&lt;br /&gt;
// Conditions for which records should be updated.&lt;br /&gt;
$conditions = array(&lt;br /&gt;
    &#039;user_id=42&#039;, &lt;br /&gt;
    &#039;profile_key=\&#039;custom.message\&#039;&#039;);&lt;br /&gt;
&lt;br /&gt;
$query-&amp;gt;update($db-&amp;gt;quoteName(&#039;#__user_profiles&#039;))-&amp;gt;set($fields)-&amp;gt;where($conditions);&lt;br /&gt;
&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
try {&lt;br /&gt;
    $result = $db-&amp;gt;query(); // Use $db-&amp;gt;execute() for Joomla 3.0.&lt;br /&gt;
catch (Exception $e) {&lt;br /&gt;
    // Catch the error.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Using an Object =====&lt;br /&gt;
&lt;br /&gt;
Like insertObject, the JDatabaseDriver class provides a convenience method for updating an object.&lt;br /&gt;
&lt;br /&gt;
Below we will update our custom table with new values using an existing id primary key:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Create an object for the record we are going to update.&lt;br /&gt;
$object = new stdClass();&lt;br /&gt;
&lt;br /&gt;
// Must be a valid primary key value.&lt;br /&gt;
$object-&amp;gt;id = 1;&lt;br /&gt;
$object-&amp;gt;title = &#039;My Custom Record&#039;;&lt;br /&gt;
$object-&amp;gt;description = &#039;A custom record being updated in the database.&#039;;&lt;br /&gt;
&lt;br /&gt;
try {&lt;br /&gt;
    // Update their details in the users table using id as the primary key.&lt;br /&gt;
    $result = JFactory::getDbo()-&amp;gt;updateObject(&#039;#__custom_table&#039;, $object, &#039;id&#039;);&lt;br /&gt;
} catch (Exception $e) {&lt;br /&gt;
    // catch the error.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Just like insertObject, updateObject takes care of escaping table names for us.&lt;br /&gt;
&lt;br /&gt;
The updateObject method will throw a RuntimeException if there is a problem inserting the record into the database table.&lt;br /&gt;
&lt;br /&gt;
We need to ensure that the record already exists before attempting to update it, so we would probably add some kind of record check before executing the updateObject method.&lt;br /&gt;
&lt;br /&gt;
====Deleting a Record====&lt;br /&gt;
&lt;br /&gt;
Just as there are select, insert and update method calls, there is also a delete method for remove records from the database.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// delete all custom keys for user 1001.&lt;br /&gt;
$conditions = array(&lt;br /&gt;
    &#039;user_id=1001&#039;, &lt;br /&gt;
    &#039;profile_key LIKE \&#039;custom.%\&#039;&#039;);&lt;br /&gt;
&lt;br /&gt;
$query-&amp;gt;delete($db-&amp;gt;quoteName(&#039;#__user_profiles&#039;));&lt;br /&gt;
$query-&amp;gt;where($conditions);&lt;br /&gt;
&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
try {&lt;br /&gt;
   $result = $db-&amp;gt;query(); // $db-&amp;gt;execute(); for Joomla 3.0.&lt;br /&gt;
} catch (Exception $e) {&lt;br /&gt;
   // catch the error.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Transactions===&lt;br /&gt;
&lt;br /&gt;
The Joomla 12.1 Framework introduces SQL transactions (where supported) via the JDatabaseDriver&#039;s transactionStart, transactionCommit and transactionRollback. This supersedes the queryBatch method which was introduced in 11.1.&lt;br /&gt;
&lt;br /&gt;
IMPORTANT NOTE: Transactions can only be used against transaction-aware storage engine such as InnoDB (many tables in Joomla still use the MyISAM storage engine which cannot support transactions).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
try {&lt;br /&gt;
    $db-&amp;gt;transactionStart();&lt;br /&gt;
	&lt;br /&gt;
    $query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
    &lt;br /&gt;
    $values = array($db-&amp;gt;quote(&#039;TEST_CONSTANT&#039;), $db-&amp;gt;quote(&#039;Custom&#039;), $db-&amp;gt;quote(&#039;/path/to/translation.ini&#039;));&lt;br /&gt;
    &lt;br /&gt;
    $query-&amp;gt;insert($db-&amp;gt;quoteName(&#039;#__overrider&#039;));&lt;br /&gt;
    $query-&amp;gt;columns($db-&amp;gt;quoteName(array(&#039;constant&#039;, &#039;string&#039;, &#039;file&#039;)));&lt;br /&gt;
    $query-&amp;gt;values(implode(&#039;,&#039;,$values));&lt;br /&gt;
&lt;br /&gt;
    $db-&amp;gt;setQuery($query);&lt;br /&gt;
    $result = $db-&amp;gt;query();&lt;br /&gt;
&lt;br /&gt;
    $db-&amp;gt;transactionCommit();&lt;br /&gt;
} catch (Exception $e) {&lt;br /&gt;
    $db-&amp;gt;transactionRollback();&lt;br /&gt;
    // catch any database errors.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Anything between the transactionStart and transactionCommit methods are not executed until transactionCommit is called. If an exception occurs, we can roll back the changes using the transactionRollback method. This allows us to return the database to the original state if a problem occurs even though we may execute a number of changes to the database&#039;s tables.&lt;/div&gt;</summary>
		<author><name>Haydenyoung</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J2.5:Accessing_the_database_using_JDatabase&amp;diff=77656</id>
		<title>J2.5:Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J2.5:Accessing_the_database_using_JDatabase&amp;diff=77656"/>
		<updated>2012-11-20T16:50:50Z</updated>

		<summary type="html">&lt;p&gt;Haydenyoung: Transactions.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{inuse}}&lt;br /&gt;
&lt;br /&gt;
Joomla provides a sophisticated database abstraction layer to simplify the usage for third party developers. New versions of the Joomla Platform API provide additional functionality which extends the database layer further, and includes features such as connectors to a greater variety of database servers and the query chaining to improve readability of connection code and simplify SQL coding.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
&lt;br /&gt;
Joomla can use different kinds of SQL database systems and run in a variety of environments with different table-prefixes. In addition to these functions, the class automatically creates the database connection. Besides instantiating the object you need just two lines of code to get a result from the database in a variety of formats. Using the Joomla database layer ensures a maximum of compatibility and flexibility for your extension.&lt;br /&gt;
&lt;br /&gt;
===The Connection===&lt;br /&gt;
&lt;br /&gt;
Joomla supports a generic interface for connecting to the database, hiding the intricacies of a specific database server from the Framework developer, thus making Joomla code easier to port from one platform to another.&lt;br /&gt;
&lt;br /&gt;
To obtain a database connection, use the [http://api.joomla.org/Joomla-Platform/JFactory.html#getDbo JFactory getDbo] static method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The getDbo method will return a reference to a high-level abstract class called [http://api.joomla.org/Joomla-Platform/Database/JDatabaseDriver.html JDatabaseDriver]; the Joomla Framework hides the specifics of the database server, which is loaded dynamically by getDbo via Joomla&#039;s configuration file.&lt;br /&gt;
&lt;br /&gt;
Alternatively, if your class inherits from the JModel (or a decendent class) getDbo method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = $this-&amp;gt;getDbo();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{JVer|3.0}} users should note that the JModel class has been renamed JModelLegacy and will eventually be deprecated in favour of the class JModelBase. Therefore, any Joomla 3.0 core or extension code should use JFactory::getDbo() to obtain a database connection.&lt;br /&gt;
&lt;br /&gt;
===The Query===&lt;br /&gt;
&lt;br /&gt;
Joomla&#039;s database querying has changed since the new Joomla Framework was introduced  &amp;quot;query chaining&amp;quot; is now the recommended method for building database queries (although string queries are still supported).&lt;br /&gt;
&lt;br /&gt;
Query chaining refers to a method of connecting a number of methods, one after the other, with each method returning an object that can support the next method, improving readability and simplifying code.&lt;br /&gt;
&lt;br /&gt;
To obtain a new instance of the JDatabaseQuery class we use the JDatabaseDriver getQuery method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The JDatabaseDriver::getQuery takes an optional argument, $new, which can be true or false (the default being false).&lt;br /&gt;
&lt;br /&gt;
To query our data source we can call a number of JDatabaseQuery methods; these methods encapsulate the data source&#039;s query language (in most cases SQL), hiding query-specific syntax from the developer and increasing the portability of the developer&#039;s source code. &lt;br /&gt;
&lt;br /&gt;
Some of the more frequently used methods include; select, from, join, where and order. There are also methods such as insert, update and delete for modifying records in the data store. By chaining these and other method calls, you can create almost any query against your data store without compromising portability of your code.&lt;br /&gt;
&lt;br /&gt;
====Selecting Records from a Single Table====&lt;br /&gt;
&lt;br /&gt;
Below is an example of creating a database query using the JDatabaseQuery class. Using the select, from, where and order methods, we can create queries which are flexible, easily readable and portable:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Select all records from the user profile table where key begins with &amp;quot;custom.&amp;quot;.&lt;br /&gt;
// Order it by the ordering field.&lt;br /&gt;
$query-&amp;gt;select(array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;));&lt;br /&gt;
$query-&amp;gt;from(&#039;#__user_profiles&#039;);&lt;br /&gt;
$query-&amp;gt;where(&#039;profile_key LIKE \&#039;custom.%\&#039;&#039;);&lt;br /&gt;
$query-&amp;gt;order(&#039;ordering ASC&#039;);&lt;br /&gt;
&lt;br /&gt;
// Reset the query using our newly populated query object.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
// Load the results as a list of stdClass objects.&lt;br /&gt;
$results = $db-&amp;gt;loadObjectList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The query can also be chained to simplify further:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;select(array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;))&lt;br /&gt;
    -&amp;gt;from(&#039;#__user_profiles&#039;)&lt;br /&gt;
    -&amp;gt;where(&#039;profile_key LIKE \&#039;custom.%\&#039;&#039;)&lt;br /&gt;
    -&amp;gt;order(&#039;ordering ASC&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Chaining can become useful when queries become longer and more complex.&lt;br /&gt;
&lt;br /&gt;
====Selecting Records from Multiple Tables====&lt;br /&gt;
&lt;br /&gt;
Using the JDatabaseQuery&#039;s [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#join join] methods, we can select records from multiple related tables. The generic &amp;quot;join&amp;quot; method takes two arguments; the join &amp;quot;type&amp;quot; (inner, outer, left, right) and the join condition. In the following example you will notice that we can use all of the keywords we would normally use if we were writing a native SQL query, including the AS keyword for aliasing tables and the ON keyword for creating relationships between tables. Also note that the table alias is used in all methods which reference table columns (I.e. select, where, order).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Select all articles for users who have a username which starts with &#039;a&#039;.&lt;br /&gt;
// Order it by the created date.&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;select(array(&#039;a.*&#039;, &#039;b.username&#039;, &#039;b.name&#039;))&lt;br /&gt;
    -&amp;gt;from(&#039;#__content AS a&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;INNER&#039;, &#039;#__users AS b ON (a.created_by = b.id)&#039;)&lt;br /&gt;
    -&amp;gt;where(&#039;b.username LIKE \&#039;a%\&#039;&#039;)&lt;br /&gt;
    -&amp;gt;order(&#039;a.created DESC&#039;);&lt;br /&gt;
&lt;br /&gt;
// Reset the query using our newly populated query object.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
// Load the results as a list of stdClass objects.&lt;br /&gt;
$results = $db-&amp;gt;loadObjectList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The join method above enables us to query both the content and user tables, retrieving articles with their author details. There are also convenience methods for [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#innerJoin inner], [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#leftJoin left], [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#rightJoin right] and [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#outerJoin outer] joins.&lt;br /&gt;
&lt;br /&gt;
We can use multiple joins to query across more than two tables:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;select(array(&#039;a.*&#039;, &#039;b.username&#039;, &#039;b.name&#039;, &#039;c.*&#039;, &#039;d.*&#039;))&lt;br /&gt;
    -&amp;gt;from(&#039;#__content AS a&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;INNER&#039;, &#039;#__users AS b ON (a.created_by = b.id)&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;LEFT&#039;, &#039;#__user_profiles AS c ON (b.id = c.user_id)&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;RIGHT&#039;, &#039;#__categories AS d ON (a.catid = d.id)&#039;)&lt;br /&gt;
    -&amp;gt;where(&#039;b.username LIKE \&#039;a%\&#039;&#039;)&lt;br /&gt;
    -&amp;gt;order(&#039;a.created DESC&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice how chaining makes the source code much more readable for these longer queries.&lt;br /&gt;
&lt;br /&gt;
====Inserting a Record====&lt;br /&gt;
&lt;br /&gt;
=====Using SQL=====&lt;br /&gt;
&lt;br /&gt;
The JDatabaseQuery class provides a number of methods for building insert queries, the most common being [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#insert insert], [http://api.joomla.org/11.4/Joomla-Platform/Database/JDatabaseQuery.html#columns columns] and [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#values values].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Insert columns.&lt;br /&gt;
$columns = array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;);&lt;br /&gt;
&lt;br /&gt;
// Insert values.&lt;br /&gt;
$values = array(1001, $db-&amp;gt;quote(&#039;custom.message&#039;), $db-&amp;gt;quote(&#039;Inserting a record using insert()&#039;), 1);&lt;br /&gt;
&lt;br /&gt;
// Prepare the insert query.&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;insert($db-&amp;gt;quoteName(&#039;#__user_profiles&#039;))&lt;br /&gt;
    -&amp;gt;columns($db-&amp;gt;quoteName($columns))&lt;br /&gt;
    -&amp;gt;values(implode(&#039;,&#039;, $values));&lt;br /&gt;
&lt;br /&gt;
// Reset the query using our newly populated query object.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since Joomla 3.0 there has been a change in how the Joomla database class executes a query.&lt;br /&gt;
&lt;br /&gt;
When executing a query in the Joomla Framework 11.4 we use the JDatabase&#039;s [http://api.joomla.org/11.4/Joomla-Platform/Database/JDatabase.html#query query] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
try {&lt;br /&gt;
    // Execute the query in Joomla 2.5.&lt;br /&gt;
    $result = $db-&amp;gt;query();&lt;br /&gt;
} catch (Exception $e) {&lt;br /&gt;
    // catch any database errors.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For Joomla Framework 12.1 and higher, we use the JDatabaseDriver&#039;s [http://api.joomla.org/Joomla-Platform/Database/JDatabaseDriver.html#execute execute] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
try {&lt;br /&gt;
    // Execute the query in Joomla 3.0.&lt;br /&gt;
    $result = $db-&amp;gt;execute();&lt;br /&gt;
} catch (Exception $e) {&lt;br /&gt;
    // catch any database errors.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since the phase out of PHP 4.x support, PHP&#039;s exception model has been widely implemented in the Joomla Framework. Therefore, we can wrap our database execution in a try...catch block to catch any unexpected database errors that may arise. Any database-specific errors will be thrown using an instance of the [http://api.joomla.org/Joomla-Platform/Database/JDatabaseException.html JDatabaseException] class although it is best practice to catch the generic Exception object from which JDatabaseException inherits.&lt;br /&gt;
&lt;br /&gt;
=====Using an Object=====&lt;br /&gt;
&lt;br /&gt;
The JDatabaseDriver also class provides us with a convenience method for saving an object directly to the database allowing us to add a record to a table without writing a single line of SQL.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Create and populate an object.&lt;br /&gt;
$profile = new stdClass();&lt;br /&gt;
$profile-&amp;gt;user_id = 1001;&lt;br /&gt;
$profile-&amp;gt;profile_key=&#039;custom.message&#039;;&lt;br /&gt;
$profile-&amp;gt;profile_value=&#039;Inserting a record using insertObject()&#039;;&lt;br /&gt;
$profile-&amp;gt;ordering=1;&lt;br /&gt;
&lt;br /&gt;
try {&lt;br /&gt;
    // Insert the object into the user profile table.&lt;br /&gt;
    $result = JFactory::getDbo()-&amp;gt;insertObject(&#039;#__user_profiles&#039;, $profile);&lt;br /&gt;
catch (Exception $e) {&lt;br /&gt;
    // catch any errors.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice here that we do not need to escape the table name; the insertObject method does this for us.&lt;br /&gt;
&lt;br /&gt;
The insertObject method will throw a [http://php.net/manual/en/class.runtimeexception.php RuntimeException] if there is a problem inserting the record into the database table.&lt;br /&gt;
&lt;br /&gt;
We need to ensure that the record does not exist before attempting to insert it, so adding some kind of record check before executing the insertObject method would be recommended.&lt;br /&gt;
&lt;br /&gt;
====Updating a Record====&lt;br /&gt;
&lt;br /&gt;
=====Using SQL=====&lt;br /&gt;
&lt;br /&gt;
The JDatabaseQuery class also provides methods for building update queries, in particular [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#update update] and [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#set set]. We also reuse another method which we used when creating select statements, the where method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Fields to update.&lt;br /&gt;
$fields = array(&lt;br /&gt;
    &#039;profile_value=\&#039;Updating custom message for user 1001.\&#039;&#039;,&lt;br /&gt;
    &#039;ordering=2&#039;);&lt;br /&gt;
&lt;br /&gt;
// Conditions for which records should be updated.&lt;br /&gt;
$conditions = array(&lt;br /&gt;
    &#039;user_id=42&#039;, &lt;br /&gt;
    &#039;profile_key=\&#039;custom.message\&#039;&#039;);&lt;br /&gt;
&lt;br /&gt;
$query-&amp;gt;update($db-&amp;gt;quoteName(&#039;#__user_profiles&#039;))-&amp;gt;set($fields)-&amp;gt;where($conditions);&lt;br /&gt;
&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
try {&lt;br /&gt;
    $result = $db-&amp;gt;query(); // Use $db-&amp;gt;execute() for Joomla 3.0.&lt;br /&gt;
catch (Exception $e) {&lt;br /&gt;
    // Catch the error.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Using an Object =====&lt;br /&gt;
&lt;br /&gt;
Like insertObject, the JDatabaseDriver class provides a convenience method for updating an object.&lt;br /&gt;
&lt;br /&gt;
Below we will update our custom table with new values using an existing id primary key:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Create an object for the record we are going to update.&lt;br /&gt;
$object = new stdClass();&lt;br /&gt;
&lt;br /&gt;
// Must be a valid primary key value.&lt;br /&gt;
$object-&amp;gt;id = 1;&lt;br /&gt;
$object-&amp;gt;title = &#039;My Custom Record&#039;;&lt;br /&gt;
$object-&amp;gt;description = &#039;A custom record being updated in the database.&#039;;&lt;br /&gt;
&lt;br /&gt;
try {&lt;br /&gt;
    // Update their details in the users table using id as the primary key.&lt;br /&gt;
    $result = JFactory::getDbo()-&amp;gt;updateObject(&#039;#__custom_table&#039;, $object, &#039;id&#039;);&lt;br /&gt;
} catch (Exception $e) {&lt;br /&gt;
    // catch the error.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Just like insertObject, updateObject takes care of escaping table names for us.&lt;br /&gt;
&lt;br /&gt;
The updateObject method will throw a RuntimeException if there is a problem inserting the record into the database table.&lt;br /&gt;
&lt;br /&gt;
We need to ensure that the record already exists before attempting to update it, so we would probably add some kind of record check before executing the updateObject method.&lt;br /&gt;
&lt;br /&gt;
====Deleting a Record====&lt;br /&gt;
&lt;br /&gt;
Just as there are select, insert and update method calls, there is also a delete method for remove records from the database.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// delete all custom keys for user 1001.&lt;br /&gt;
$conditions = array(&lt;br /&gt;
    &#039;user_id=1001&#039;, &lt;br /&gt;
    &#039;profile_key LIKE \&#039;custom.%\&#039;&#039;);&lt;br /&gt;
&lt;br /&gt;
$query-&amp;gt;delete($db-&amp;gt;quoteName(&#039;#__user_profiles&#039;));&lt;br /&gt;
$query-&amp;gt;where($conditions);&lt;br /&gt;
&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
try {&lt;br /&gt;
   $result = $db-&amp;gt;query(); // $db-&amp;gt;execute(); for Joomla 3.0.&lt;br /&gt;
} catch (Exception $e) {&lt;br /&gt;
   // catch the error.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Transactions===&lt;br /&gt;
&lt;br /&gt;
The Joomla 12.1 Framework introduces SQL transactions (where supported) via the JDatabaseDriver&#039;s transactionStart, transactionCommit and transactionRollback. This supersedes the queryBatch method which was introduced in 11.1.&lt;br /&gt;
&lt;br /&gt;
IMPORTANT NOTE: Transactions can only be used against transaction-aware storage engine such as InnoDB (many tables in Joomla still use the MyISAM storage engine which cannot support transactions).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
try {&lt;br /&gt;
    $db-&amp;gt;transactionStart();&lt;br /&gt;
	&lt;br /&gt;
    $query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
    &lt;br /&gt;
    $values = array($db-&amp;gt;quote(&#039;TEST_CONSTANT&#039;), $db-&amp;gt;quote(&#039;Custom&#039;), $db-&amp;gt;quote(&#039;/path/to/translation.ini&#039;));&lt;br /&gt;
    &lt;br /&gt;
    $query-&amp;gt;insert($db-&amp;gt;quoteName(&#039;#__overrider&#039;));&lt;br /&gt;
    $query-&amp;gt;columns($db-&amp;gt;quoteName(array(&#039;constant&#039;, &#039;string&#039;, &#039;file&#039;)));&lt;br /&gt;
    $query-&amp;gt;values(implode(&#039;,&#039;,$values));&lt;br /&gt;
&lt;br /&gt;
    $db-&amp;gt;setQuery($query);&lt;br /&gt;
    $result = $db-&amp;gt;query();&lt;br /&gt;
&lt;br /&gt;
    $db-&amp;gt;transactionCommit();&lt;br /&gt;
} catch (Exception $e) {&lt;br /&gt;
    $db-&amp;gt;transactionRollback();&lt;br /&gt;
    // catch any database errors.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Anything between the transactionStart and transactionCommit methods are not executed until transactionCommit is called. If an exception occurs, we can roll back the changes using the transactionRollback method. This allows us to return the database to the original state if a problem occurs even though we may execute a number of changes to the database&#039;s tables.&lt;/div&gt;</summary>
		<author><name>Haydenyoung</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J2.5:Accessing_the_database_using_JDatabase&amp;diff=77654</id>
		<title>J2.5:Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J2.5:Accessing_the_database_using_JDatabase&amp;diff=77654"/>
		<updated>2012-11-20T15:21:29Z</updated>

		<summary type="html">&lt;p&gt;Haydenyoung: /* Using SQL */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{inuse}}&lt;br /&gt;
&lt;br /&gt;
Joomla provides a sophisticated database abstraction layer to simplify the usage for third party developers. New versions of the Joomla Platform API provide additional functionality which extends the database layer further, and includes features such as connectors to a greater variety of database servers and the query chaining to improve readability of connection code and simplify SQL coding.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
&lt;br /&gt;
Joomla can use different kinds of SQL database systems and run in a variety of environments with different table-prefixes. In addition to these functions, the class automatically creates the database connection. Besides instantiating the object you need just two lines of code to get a result from the database in a variety of formats. Using the Joomla database layer ensures a maximum of compatibility and flexibility for your extension.&lt;br /&gt;
&lt;br /&gt;
===The Connection===&lt;br /&gt;
&lt;br /&gt;
Joomla supports a generic interface for connecting to the database, hiding the intricacies of a specific database server from the Framework developer, thus making Joomla code easier to port from one platform to another.&lt;br /&gt;
&lt;br /&gt;
To obtain a database connection, use the [http://api.joomla.org/Joomla-Platform/JFactory.html#getDbo JFactory getDbo] static method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The getDbo method will return a reference to a high-level abstract class called [http://api.joomla.org/Joomla-Platform/Database/JDatabaseDriver.html JDatabaseDriver]; the Joomla Framework hides the specifics of the database server, which is loaded dynamically by getDbo via Joomla&#039;s configuration file.&lt;br /&gt;
&lt;br /&gt;
Alternatively, if your class inherits from the JModel (or a decendent class) getDbo method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = $this-&amp;gt;getDbo();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{JVer|3.0}} users should note that the JModel class has been renamed JModelLegacy and will eventually be deprecated in favour of the class JModelBase. Therefore, any Joomla 3.0 core or extension code should use JFactory::getDbo() to obtain a database connection.&lt;br /&gt;
&lt;br /&gt;
===The Query===&lt;br /&gt;
&lt;br /&gt;
Joomla&#039;s database querying has changed since the new Joomla Framework was introduced  &amp;quot;query chaining&amp;quot; is now the recommended method for building database queries (although string queries are still supported).&lt;br /&gt;
&lt;br /&gt;
Query chaining refers to a method of connecting a number of methods, one after the other, with each method returning an object that can support the next method, improving readability and simplifying code.&lt;br /&gt;
&lt;br /&gt;
To obtain a new instance of the JDatabaseQuery class we use the JDatabaseDriver getQuery method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The JDatabaseDriver::getQuery takes an optional argument, $new, which can be true or false (the default being false).&lt;br /&gt;
&lt;br /&gt;
To query our data source we can call a number of JDatabaseQuery methods; these methods encapsulate the data source&#039;s query language (in most cases SQL), hiding query-specific syntax from the developer and increasing the portability of the developer&#039;s source code. &lt;br /&gt;
&lt;br /&gt;
Some of the more frequently used methods include; select, from, join, where and order. There are also methods such as insert, update and delete for modifying records in the data store. By chaining these and other method calls, you can create almost any query against your data store without compromising portability of your code.&lt;br /&gt;
&lt;br /&gt;
====Selecting Records from a Single Table====&lt;br /&gt;
&lt;br /&gt;
Below is an example of creating a database query using the JDatabaseQuery class. Using the select, from, where and order methods, we can create queries which are flexible, easily readable and portable:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Select all records from the user profile table where key begins with &amp;quot;custom.&amp;quot;.&lt;br /&gt;
// Order it by the ordering field.&lt;br /&gt;
$query-&amp;gt;select(array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;));&lt;br /&gt;
$query-&amp;gt;from(&#039;#__user_profiles&#039;);&lt;br /&gt;
$query-&amp;gt;where(&#039;profile_key LIKE \&#039;custom.%\&#039;&#039;);&lt;br /&gt;
$query-&amp;gt;order(&#039;ordering ASC&#039;);&lt;br /&gt;
&lt;br /&gt;
// Reset the query using our newly populated query object.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
// Load the results as a list of stdClass objects.&lt;br /&gt;
$results = $db-&amp;gt;loadObjectList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The query can also be chained to simplify further:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;select(array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;))&lt;br /&gt;
    -&amp;gt;from(&#039;#__user_profiles&#039;)&lt;br /&gt;
    -&amp;gt;where(&#039;profile_key LIKE \&#039;custom.%\&#039;&#039;)&lt;br /&gt;
    -&amp;gt;order(&#039;ordering ASC&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Chaining can become useful when queries become longer and more complex.&lt;br /&gt;
&lt;br /&gt;
====Selecting Records from Multiple Tables====&lt;br /&gt;
&lt;br /&gt;
Using the JDatabaseQuery&#039;s [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#join join] methods, we can select records from multiple related tables. The generic &amp;quot;join&amp;quot; method takes two arguments; the join &amp;quot;type&amp;quot; (inner, outer, left, right) and the join condition. In the following example you will notice that we can use all of the keywords we would normally use if we were writing a native SQL query, including the AS keyword for aliasing tables and the ON keyword for creating relationships between tables. Also note that the table alias is used in all methods which reference table columns (I.e. select, where, order).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Select all articles for users who have a username which starts with &#039;a&#039;.&lt;br /&gt;
// Order it by the created date.&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;select(array(&#039;a.*&#039;, &#039;b.username&#039;, &#039;b.name&#039;))&lt;br /&gt;
    -&amp;gt;from(&#039;#__content AS a&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;INNER&#039;, &#039;#__users AS b ON (a.created_by = b.id)&#039;)&lt;br /&gt;
    -&amp;gt;where(&#039;b.username LIKE \&#039;a%\&#039;&#039;)&lt;br /&gt;
    -&amp;gt;order(&#039;a.created DESC&#039;);&lt;br /&gt;
&lt;br /&gt;
// Reset the query using our newly populated query object.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
// Load the results as a list of stdClass objects.&lt;br /&gt;
$results = $db-&amp;gt;loadObjectList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The join method above enables us to query both the content and user tables, retrieving articles with their author details. There are also convenience methods for [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#innerJoin inner], [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#leftJoin left], [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#rightJoin right] and [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#outerJoin outer] joins.&lt;br /&gt;
&lt;br /&gt;
We can use multiple joins to query across more than two tables:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;select(array(&#039;a.*&#039;, &#039;b.username&#039;, &#039;b.name&#039;, &#039;c.*&#039;, &#039;d.*&#039;))&lt;br /&gt;
    -&amp;gt;from(&#039;#__content AS a&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;INNER&#039;, &#039;#__users AS b ON (a.created_by = b.id)&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;LEFT&#039;, &#039;#__user_profiles AS c ON (b.id = c.user_id)&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;RIGHT&#039;, &#039;#__categories AS d ON (a.catid = d.id)&#039;)&lt;br /&gt;
    -&amp;gt;where(&#039;b.username LIKE \&#039;a%\&#039;&#039;)&lt;br /&gt;
    -&amp;gt;order(&#039;a.created DESC&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice how chaining makes the source code much more readable for these longer queries.&lt;br /&gt;
&lt;br /&gt;
====Inserting a Record====&lt;br /&gt;
&lt;br /&gt;
=====Using SQL=====&lt;br /&gt;
&lt;br /&gt;
The JDatabaseQuery class provides a number of methods for building insert queries, the most common being [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#insert insert], [http://api.joomla.org/11.4/Joomla-Platform/Database/JDatabaseQuery.html#columns columns] and [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#values values].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Insert columns.&lt;br /&gt;
$columns = array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;);&lt;br /&gt;
&lt;br /&gt;
// Insert values.&lt;br /&gt;
$values = array(1001, $db-&amp;gt;quote(&#039;custom.message&#039;), $db-&amp;gt;quote(&#039;Inserting a record using insert()&#039;), 1);&lt;br /&gt;
&lt;br /&gt;
// Prepare the insert query.&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;insert($db-&amp;gt;quoteName(&#039;#__user_profiles&#039;))&lt;br /&gt;
    -&amp;gt;columns($db-&amp;gt;quoteName($columns))&lt;br /&gt;
    -&amp;gt;values(implode(&#039;,&#039;, $values));&lt;br /&gt;
&lt;br /&gt;
// Reset the query using our newly populated query object.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since Joomla 3.0 there has been a change in how the Joomla database class executes a query.&lt;br /&gt;
&lt;br /&gt;
When executing a query in the Joomla Framework 11.4 we use the JDatabase&#039;s [http://api.joomla.org/11.4/Joomla-Platform/Database/JDatabase.html#query query] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
try {&lt;br /&gt;
    // Execute the query in Joomla 2.5.&lt;br /&gt;
    $result = $db-&amp;gt;query();&lt;br /&gt;
} catch (Exception $e) {&lt;br /&gt;
    // catch any database errors.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For Joomla Framework 12.1 and higher, we use the JDatabaseDriver&#039;s [http://api.joomla.org/Joomla-Platform/Database/JDatabaseDriver.html#execute execute] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
try {&lt;br /&gt;
    // Execute the query in Joomla 3.0.&lt;br /&gt;
    $result = $db-&amp;gt;execute();&lt;br /&gt;
} catch (Exception $e) {&lt;br /&gt;
    // catch any database errors.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since the phase out of PHP 4.x support, PHP&#039;s exception model has been widely implemented in the Joomla Framework. Therefore, we can wrap our database execution in a try...catch block to catch any unexpected database errors that may arise. Any database-specific errors will be thrown using an instance of the [http://api.joomla.org/Joomla-Platform/Database/JDatabaseException.html JDatabaseException] class although it is best practice to catch the generic Exception object from which JDatabaseException inherits.&lt;br /&gt;
&lt;br /&gt;
=====Using an Object=====&lt;br /&gt;
&lt;br /&gt;
The JDatabaseDriver also class provides us with a convenience method for saving an object directly to the database allowing us to add a record to a table without writing a single line of SQL.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Create and populate an object.&lt;br /&gt;
$profile = new stdClass();&lt;br /&gt;
$profile-&amp;gt;user_id = 1001;&lt;br /&gt;
$profile-&amp;gt;profile_key=&#039;custom.message&#039;;&lt;br /&gt;
$profile-&amp;gt;profile_value=&#039;Inserting a record using insertObject()&#039;;&lt;br /&gt;
$profile-&amp;gt;ordering=1;&lt;br /&gt;
&lt;br /&gt;
try {&lt;br /&gt;
    // Insert the object into the user profile table.&lt;br /&gt;
    $result = JFactory::getDbo()-&amp;gt;insertObject(&#039;#__user_profiles&#039;, $profile);&lt;br /&gt;
catch (Exception $e) {&lt;br /&gt;
    // catch any errors.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice here that we do not need to escape the table name; the insertObject method does this for us.&lt;br /&gt;
&lt;br /&gt;
The insertObject method will throw a [http://php.net/manual/en/class.runtimeexception.php RuntimeException] if there is a problem inserting the record into the database table.&lt;br /&gt;
&lt;br /&gt;
We need to ensure that the record does not exist before attempting to insert it, so adding some kind of record check before executing the insertObject method would be recommended.&lt;br /&gt;
&lt;br /&gt;
====Updating a Record====&lt;br /&gt;
&lt;br /&gt;
=====Using SQL=====&lt;br /&gt;
&lt;br /&gt;
The JDatabaseQuery class also provides methods for building update queries, in particular [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#update update] and [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#set set]. We also reuse another method which we used when creating select statements, the where method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Fields to update.&lt;br /&gt;
$fields = array(&lt;br /&gt;
    &#039;profile_value=\&#039;Updating custom message for user 1001.\&#039;&#039;,&lt;br /&gt;
    &#039;ordering=2&#039;);&lt;br /&gt;
&lt;br /&gt;
// Conditions for which records should be updated.&lt;br /&gt;
$conditions = array(&lt;br /&gt;
    &#039;user_id=42&#039;, &lt;br /&gt;
    &#039;profile_key=\&#039;custom.message\&#039;&#039;);&lt;br /&gt;
&lt;br /&gt;
$query-&amp;gt;update($db-&amp;gt;quoteName(&#039;#__user_profiles&#039;))-&amp;gt;set($fields)-&amp;gt;where($conditions);&lt;br /&gt;
&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
try {&lt;br /&gt;
    $result = $db-&amp;gt;query(); // Use $db-&amp;gt;execute() for Joomla 3.0.&lt;br /&gt;
catch (Exception $e) {&lt;br /&gt;
    // Catch the error.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Using an Object =====&lt;br /&gt;
&lt;br /&gt;
Like insertObject, the JDatabaseDriver class provides a convenience method for updating an object.&lt;br /&gt;
&lt;br /&gt;
Below we will update our custom table with new values using an existing id primary key:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Create an object for the record we are going to update.&lt;br /&gt;
$object = new stdClass();&lt;br /&gt;
&lt;br /&gt;
// Must be a valid primary key value.&lt;br /&gt;
$object-&amp;gt;id = 1;&lt;br /&gt;
$object-&amp;gt;title = &#039;My Custom Record&#039;;&lt;br /&gt;
$object-&amp;gt;description = &#039;A custom record being updated in the database.&#039;;&lt;br /&gt;
&lt;br /&gt;
try {&lt;br /&gt;
    // Update their details in the users table using id as the primary key.&lt;br /&gt;
    $result = JFactory::getDbo()-&amp;gt;updateObject(&#039;#__custom_table&#039;, $object, &#039;id&#039;);&lt;br /&gt;
} catch (Exception $e) {&lt;br /&gt;
    // catch the error.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Just like insertObject, updateObject takes care of escaping table names for us.&lt;br /&gt;
&lt;br /&gt;
The updateObject method will throw a RuntimeException if there is a problem inserting the record into the database table.&lt;br /&gt;
&lt;br /&gt;
We need to ensure that the record already exists before attempting to update it, so we would probably add some kind of record check before executing the updateObject method.&lt;br /&gt;
&lt;br /&gt;
====Deleting a Record====&lt;br /&gt;
&lt;br /&gt;
Just as there are select, insert and update method calls, there is also a delete method for remove records from the database.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// delete all custom keys for user 1001.&lt;br /&gt;
$conditions = array(&lt;br /&gt;
    &#039;user_id=1001&#039;, &lt;br /&gt;
    &#039;profile_key LIKE \&#039;custom.%\&#039;&#039;);&lt;br /&gt;
&lt;br /&gt;
$query-&amp;gt;delete($db-&amp;gt;quoteName(&#039;#__user_profiles&#039;));&lt;br /&gt;
$query-&amp;gt;where($conditions);&lt;br /&gt;
&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
try {&lt;br /&gt;
   $result = $db-&amp;gt;query(); // $db-&amp;gt;execute(); for Joomla 3.0.&lt;br /&gt;
} catch (Exception $e) {&lt;br /&gt;
   // catch the error.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Haydenyoung</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J2.5:Accessing_the_database_using_JDatabase&amp;diff=77653</id>
		<title>J2.5:Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J2.5:Accessing_the_database_using_JDatabase&amp;diff=77653"/>
		<updated>2012-11-20T15:21:00Z</updated>

		<summary type="html">&lt;p&gt;Haydenyoung: /* Deleting a Record */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{inuse}}&lt;br /&gt;
&lt;br /&gt;
Joomla provides a sophisticated database abstraction layer to simplify the usage for third party developers. New versions of the Joomla Platform API provide additional functionality which extends the database layer further, and includes features such as connectors to a greater variety of database servers and the query chaining to improve readability of connection code and simplify SQL coding.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
&lt;br /&gt;
Joomla can use different kinds of SQL database systems and run in a variety of environments with different table-prefixes. In addition to these functions, the class automatically creates the database connection. Besides instantiating the object you need just two lines of code to get a result from the database in a variety of formats. Using the Joomla database layer ensures a maximum of compatibility and flexibility for your extension.&lt;br /&gt;
&lt;br /&gt;
===The Connection===&lt;br /&gt;
&lt;br /&gt;
Joomla supports a generic interface for connecting to the database, hiding the intricacies of a specific database server from the Framework developer, thus making Joomla code easier to port from one platform to another.&lt;br /&gt;
&lt;br /&gt;
To obtain a database connection, use the [http://api.joomla.org/Joomla-Platform/JFactory.html#getDbo JFactory getDbo] static method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The getDbo method will return a reference to a high-level abstract class called [http://api.joomla.org/Joomla-Platform/Database/JDatabaseDriver.html JDatabaseDriver]; the Joomla Framework hides the specifics of the database server, which is loaded dynamically by getDbo via Joomla&#039;s configuration file.&lt;br /&gt;
&lt;br /&gt;
Alternatively, if your class inherits from the JModel (or a decendent class) getDbo method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = $this-&amp;gt;getDbo();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{JVer|3.0}} users should note that the JModel class has been renamed JModelLegacy and will eventually be deprecated in favour of the class JModelBase. Therefore, any Joomla 3.0 core or extension code should use JFactory::getDbo() to obtain a database connection.&lt;br /&gt;
&lt;br /&gt;
===The Query===&lt;br /&gt;
&lt;br /&gt;
Joomla&#039;s database querying has changed since the new Joomla Framework was introduced  &amp;quot;query chaining&amp;quot; is now the recommended method for building database queries (although string queries are still supported).&lt;br /&gt;
&lt;br /&gt;
Query chaining refers to a method of connecting a number of methods, one after the other, with each method returning an object that can support the next method, improving readability and simplifying code.&lt;br /&gt;
&lt;br /&gt;
To obtain a new instance of the JDatabaseQuery class we use the JDatabaseDriver getQuery method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The JDatabaseDriver::getQuery takes an optional argument, $new, which can be true or false (the default being false).&lt;br /&gt;
&lt;br /&gt;
To query our data source we can call a number of JDatabaseQuery methods; these methods encapsulate the data source&#039;s query language (in most cases SQL), hiding query-specific syntax from the developer and increasing the portability of the developer&#039;s source code. &lt;br /&gt;
&lt;br /&gt;
Some of the more frequently used methods include; select, from, join, where and order. There are also methods such as insert, update and delete for modifying records in the data store. By chaining these and other method calls, you can create almost any query against your data store without compromising portability of your code.&lt;br /&gt;
&lt;br /&gt;
====Selecting Records from a Single Table====&lt;br /&gt;
&lt;br /&gt;
Below is an example of creating a database query using the JDatabaseQuery class. Using the select, from, where and order methods, we can create queries which are flexible, easily readable and portable:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Select all records from the user profile table where key begins with &amp;quot;custom.&amp;quot;.&lt;br /&gt;
// Order it by the ordering field.&lt;br /&gt;
$query-&amp;gt;select(array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;));&lt;br /&gt;
$query-&amp;gt;from(&#039;#__user_profiles&#039;);&lt;br /&gt;
$query-&amp;gt;where(&#039;profile_key LIKE \&#039;custom.%\&#039;&#039;);&lt;br /&gt;
$query-&amp;gt;order(&#039;ordering ASC&#039;);&lt;br /&gt;
&lt;br /&gt;
// Reset the query using our newly populated query object.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
// Load the results as a list of stdClass objects.&lt;br /&gt;
$results = $db-&amp;gt;loadObjectList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The query can also be chained to simplify further:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;select(array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;))&lt;br /&gt;
    -&amp;gt;from(&#039;#__user_profiles&#039;)&lt;br /&gt;
    -&amp;gt;where(&#039;profile_key LIKE \&#039;custom.%\&#039;&#039;)&lt;br /&gt;
    -&amp;gt;order(&#039;ordering ASC&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Chaining can become useful when queries become longer and more complex.&lt;br /&gt;
&lt;br /&gt;
====Selecting Records from Multiple Tables====&lt;br /&gt;
&lt;br /&gt;
Using the JDatabaseQuery&#039;s [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#join join] methods, we can select records from multiple related tables. The generic &amp;quot;join&amp;quot; method takes two arguments; the join &amp;quot;type&amp;quot; (inner, outer, left, right) and the join condition. In the following example you will notice that we can use all of the keywords we would normally use if we were writing a native SQL query, including the AS keyword for aliasing tables and the ON keyword for creating relationships between tables. Also note that the table alias is used in all methods which reference table columns (I.e. select, where, order).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Select all articles for users who have a username which starts with &#039;a&#039;.&lt;br /&gt;
// Order it by the created date.&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;select(array(&#039;a.*&#039;, &#039;b.username&#039;, &#039;b.name&#039;))&lt;br /&gt;
    -&amp;gt;from(&#039;#__content AS a&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;INNER&#039;, &#039;#__users AS b ON (a.created_by = b.id)&#039;)&lt;br /&gt;
    -&amp;gt;where(&#039;b.username LIKE \&#039;a%\&#039;&#039;)&lt;br /&gt;
    -&amp;gt;order(&#039;a.created DESC&#039;);&lt;br /&gt;
&lt;br /&gt;
// Reset the query using our newly populated query object.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
// Load the results as a list of stdClass objects.&lt;br /&gt;
$results = $db-&amp;gt;loadObjectList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The join method above enables us to query both the content and user tables, retrieving articles with their author details. There are also convenience methods for [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#innerJoin inner], [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#leftJoin left], [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#rightJoin right] and [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#outerJoin outer] joins.&lt;br /&gt;
&lt;br /&gt;
We can use multiple joins to query across more than two tables:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;select(array(&#039;a.*&#039;, &#039;b.username&#039;, &#039;b.name&#039;, &#039;c.*&#039;, &#039;d.*&#039;))&lt;br /&gt;
    -&amp;gt;from(&#039;#__content AS a&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;INNER&#039;, &#039;#__users AS b ON (a.created_by = b.id)&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;LEFT&#039;, &#039;#__user_profiles AS c ON (b.id = c.user_id)&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;RIGHT&#039;, &#039;#__categories AS d ON (a.catid = d.id)&#039;)&lt;br /&gt;
    -&amp;gt;where(&#039;b.username LIKE \&#039;a%\&#039;&#039;)&lt;br /&gt;
    -&amp;gt;order(&#039;a.created DESC&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice how chaining makes the source code much more readable for these longer queries.&lt;br /&gt;
&lt;br /&gt;
====Inserting a Record====&lt;br /&gt;
&lt;br /&gt;
=====Using SQL=====&lt;br /&gt;
&lt;br /&gt;
The JDatabaseQuery class provides a number of methods for building insert queries, the most common being [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#insert insert], [http://api.joomla.org/11.4/Joomla-Platform/Database/JDatabaseQuery.html#columns columns] and [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#values values].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Insert columns.&lt;br /&gt;
$columns = array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;);&lt;br /&gt;
&lt;br /&gt;
// Insert values.&lt;br /&gt;
$values = array(1001, $db-&amp;gt;quote(&#039;custom.message&#039;), $db-&amp;gt;quote(&#039;Inserting a record using insert()&#039;), 1);&lt;br /&gt;
&lt;br /&gt;
// Prepare the insert query.&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;insert($db-&amp;gt;quoteName(&#039;#__user_profiles&#039;))&lt;br /&gt;
    -&amp;gt;columns($db-&amp;gt;quoteName($columns))&lt;br /&gt;
    -&amp;gt;values(implode(&#039;,&#039;, $values));&lt;br /&gt;
&lt;br /&gt;
// Reset the query using our newly populated query object.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since Joomla 3.0 there has been a change in how the Joomla database class executes a query.&lt;br /&gt;
&lt;br /&gt;
When executing a query in the Joomla Framework 11.4 we use the JDatabase&#039;s [http://api.joomla.org/11.4/Joomla-Platform/Database/JDatabase.html#query query] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
try {&lt;br /&gt;
    // Execute the query in Joomla 2.5.&lt;br /&gt;
    $result = $db-&amp;gt;query();&lt;br /&gt;
} catch (Exception $e) {&lt;br /&gt;
    // catch any database errors.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For Joomla Framework 12.1 and higher, we use the JDatabaseDriver&#039;s [http://api.joomla.org/Joomla-Platform/Database/JDatabaseDriver.html#execute execute] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
try {&lt;br /&gt;
    // Execute the query in Joomla 3.0.&lt;br /&gt;
    $result = $db-&amp;gt;execute();&lt;br /&gt;
} catch (Exception $e) {&lt;br /&gt;
    // catch any database errors.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since the phase out of PHP 4.x support, PHP&#039;s exception model has been widely implemented in the Joomla Framework. Therefore, we can wrap our database execution in a try...catch block to catch any unexpected database errors that may arise. Any database-specific errors will be thrown using an instance of the [http://api.joomla.org/Joomla-Platform/Database/JDatabaseException.html JDatabaseException] class although it is best practice to catch the generic Exception object from which JDatabaseException inherits.&lt;br /&gt;
&lt;br /&gt;
=====Using an Object=====&lt;br /&gt;
&lt;br /&gt;
The JDatabaseDriver also class provides us with a convenience method for saving an object directly to the database allowing us to add a record to a table without writing a single line of SQL.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Create and populate an object.&lt;br /&gt;
$profile = new stdClass();&lt;br /&gt;
$profile-&amp;gt;user_id = 1001;&lt;br /&gt;
$profile-&amp;gt;profile_key=&#039;custom.message&#039;;&lt;br /&gt;
$profile-&amp;gt;profile_value=&#039;Inserting a record using insertObject()&#039;;&lt;br /&gt;
$profile-&amp;gt;ordering=1;&lt;br /&gt;
&lt;br /&gt;
try {&lt;br /&gt;
    // Insert the object into the user profile table.&lt;br /&gt;
    $result = JFactory::getDbo()-&amp;gt;insertObject(&#039;#__user_profiles&#039;, $profile);&lt;br /&gt;
catch (Exception $e) {&lt;br /&gt;
    // catch any errors.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice here that we do not need to escape the table name; the insertObject method does this for us.&lt;br /&gt;
&lt;br /&gt;
The insertObject method will throw a [http://php.net/manual/en/class.runtimeexception.php RuntimeException] if there is a problem inserting the record into the database table.&lt;br /&gt;
&lt;br /&gt;
We need to ensure that the record does not exist before attempting to insert it, so adding some kind of record check before executing the insertObject method would be recommended.&lt;br /&gt;
&lt;br /&gt;
====Updating a Record====&lt;br /&gt;
&lt;br /&gt;
=====Using SQL=====&lt;br /&gt;
&lt;br /&gt;
The JDatabaseQuery class also provides methods for building update queries, in particular [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#update update] and [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#set set]. We also reuse another method which we used when creating select statements, the where method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Fields to update.&lt;br /&gt;
$fields = array(&lt;br /&gt;
    &#039;profile_value=\&#039;Updating custom message for user 1001.\&#039;&#039;,&lt;br /&gt;
    &#039;ordering=2&#039;);&lt;br /&gt;
&lt;br /&gt;
// Conditions for which records should be updated.&lt;br /&gt;
$conditions = array(&lt;br /&gt;
    &#039;user_id=42&#039;, &lt;br /&gt;
    &#039;profile_key=\&#039;custom.message\&#039;&#039;);&lt;br /&gt;
&lt;br /&gt;
$query-&amp;gt;update($db-&amp;gt;quoteName(&#039;#__user_profiles&#039;))-&amp;gt;set($fields)-&amp;gt;where($conditions);&lt;br /&gt;
&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
try {&lt;br /&gt;
    $result = $db-&amp;gt;query();&lt;br /&gt;
catch (Exception $e) {&lt;br /&gt;
    // Catch the error.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Using an Object =====&lt;br /&gt;
&lt;br /&gt;
Like insertObject, the JDatabaseDriver class provides a convenience method for updating an object.&lt;br /&gt;
&lt;br /&gt;
Below we will update our custom table with new values using an existing id primary key:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Create an object for the record we are going to update.&lt;br /&gt;
$object = new stdClass();&lt;br /&gt;
&lt;br /&gt;
// Must be a valid primary key value.&lt;br /&gt;
$object-&amp;gt;id = 1;&lt;br /&gt;
$object-&amp;gt;title = &#039;My Custom Record&#039;;&lt;br /&gt;
$object-&amp;gt;description = &#039;A custom record being updated in the database.&#039;;&lt;br /&gt;
&lt;br /&gt;
try {&lt;br /&gt;
    // Update their details in the users table using id as the primary key.&lt;br /&gt;
    $result = JFactory::getDbo()-&amp;gt;updateObject(&#039;#__custom_table&#039;, $object, &#039;id&#039;);&lt;br /&gt;
} catch (Exception $e) {&lt;br /&gt;
    // catch the error.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Just like insertObject, updateObject takes care of escaping table names for us.&lt;br /&gt;
&lt;br /&gt;
The updateObject method will throw a RuntimeException if there is a problem inserting the record into the database table.&lt;br /&gt;
&lt;br /&gt;
We need to ensure that the record already exists before attempting to update it, so we would probably add some kind of record check before executing the updateObject method.&lt;br /&gt;
&lt;br /&gt;
====Deleting a Record====&lt;br /&gt;
&lt;br /&gt;
Just as there are select, insert and update method calls, there is also a delete method for remove records from the database.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// delete all custom keys for user 1001.&lt;br /&gt;
$conditions = array(&lt;br /&gt;
    &#039;user_id=1001&#039;, &lt;br /&gt;
    &#039;profile_key LIKE \&#039;custom.%\&#039;&#039;);&lt;br /&gt;
&lt;br /&gt;
$query-&amp;gt;delete($db-&amp;gt;quoteName(&#039;#__user_profiles&#039;));&lt;br /&gt;
$query-&amp;gt;where($conditions);&lt;br /&gt;
&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
try {&lt;br /&gt;
   $result = $db-&amp;gt;query(); // $db-&amp;gt;execute(); for Joomla 3.0.&lt;br /&gt;
} catch (Exception $e) {&lt;br /&gt;
   // catch the error.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Haydenyoung</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J2.5:Accessing_the_database_using_JDatabase&amp;diff=77652</id>
		<title>J2.5:Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J2.5:Accessing_the_database_using_JDatabase&amp;diff=77652"/>
		<updated>2012-11-20T15:20:40Z</updated>

		<summary type="html">&lt;p&gt;Haydenyoung: /* Deleting a Record */ Deleting records from the database.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{inuse}}&lt;br /&gt;
&lt;br /&gt;
Joomla provides a sophisticated database abstraction layer to simplify the usage for third party developers. New versions of the Joomla Platform API provide additional functionality which extends the database layer further, and includes features such as connectors to a greater variety of database servers and the query chaining to improve readability of connection code and simplify SQL coding.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
&lt;br /&gt;
Joomla can use different kinds of SQL database systems and run in a variety of environments with different table-prefixes. In addition to these functions, the class automatically creates the database connection. Besides instantiating the object you need just two lines of code to get a result from the database in a variety of formats. Using the Joomla database layer ensures a maximum of compatibility and flexibility for your extension.&lt;br /&gt;
&lt;br /&gt;
===The Connection===&lt;br /&gt;
&lt;br /&gt;
Joomla supports a generic interface for connecting to the database, hiding the intricacies of a specific database server from the Framework developer, thus making Joomla code easier to port from one platform to another.&lt;br /&gt;
&lt;br /&gt;
To obtain a database connection, use the [http://api.joomla.org/Joomla-Platform/JFactory.html#getDbo JFactory getDbo] static method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The getDbo method will return a reference to a high-level abstract class called [http://api.joomla.org/Joomla-Platform/Database/JDatabaseDriver.html JDatabaseDriver]; the Joomla Framework hides the specifics of the database server, which is loaded dynamically by getDbo via Joomla&#039;s configuration file.&lt;br /&gt;
&lt;br /&gt;
Alternatively, if your class inherits from the JModel (or a decendent class) getDbo method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = $this-&amp;gt;getDbo();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{JVer|3.0}} users should note that the JModel class has been renamed JModelLegacy and will eventually be deprecated in favour of the class JModelBase. Therefore, any Joomla 3.0 core or extension code should use JFactory::getDbo() to obtain a database connection.&lt;br /&gt;
&lt;br /&gt;
===The Query===&lt;br /&gt;
&lt;br /&gt;
Joomla&#039;s database querying has changed since the new Joomla Framework was introduced  &amp;quot;query chaining&amp;quot; is now the recommended method for building database queries (although string queries are still supported).&lt;br /&gt;
&lt;br /&gt;
Query chaining refers to a method of connecting a number of methods, one after the other, with each method returning an object that can support the next method, improving readability and simplifying code.&lt;br /&gt;
&lt;br /&gt;
To obtain a new instance of the JDatabaseQuery class we use the JDatabaseDriver getQuery method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The JDatabaseDriver::getQuery takes an optional argument, $new, which can be true or false (the default being false).&lt;br /&gt;
&lt;br /&gt;
To query our data source we can call a number of JDatabaseQuery methods; these methods encapsulate the data source&#039;s query language (in most cases SQL), hiding query-specific syntax from the developer and increasing the portability of the developer&#039;s source code. &lt;br /&gt;
&lt;br /&gt;
Some of the more frequently used methods include; select, from, join, where and order. There are also methods such as insert, update and delete for modifying records in the data store. By chaining these and other method calls, you can create almost any query against your data store without compromising portability of your code.&lt;br /&gt;
&lt;br /&gt;
====Selecting Records from a Single Table====&lt;br /&gt;
&lt;br /&gt;
Below is an example of creating a database query using the JDatabaseQuery class. Using the select, from, where and order methods, we can create queries which are flexible, easily readable and portable:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Select all records from the user profile table where key begins with &amp;quot;custom.&amp;quot;.&lt;br /&gt;
// Order it by the ordering field.&lt;br /&gt;
$query-&amp;gt;select(array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;));&lt;br /&gt;
$query-&amp;gt;from(&#039;#__user_profiles&#039;);&lt;br /&gt;
$query-&amp;gt;where(&#039;profile_key LIKE \&#039;custom.%\&#039;&#039;);&lt;br /&gt;
$query-&amp;gt;order(&#039;ordering ASC&#039;);&lt;br /&gt;
&lt;br /&gt;
// Reset the query using our newly populated query object.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
// Load the results as a list of stdClass objects.&lt;br /&gt;
$results = $db-&amp;gt;loadObjectList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The query can also be chained to simplify further:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;select(array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;))&lt;br /&gt;
    -&amp;gt;from(&#039;#__user_profiles&#039;)&lt;br /&gt;
    -&amp;gt;where(&#039;profile_key LIKE \&#039;custom.%\&#039;&#039;)&lt;br /&gt;
    -&amp;gt;order(&#039;ordering ASC&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Chaining can become useful when queries become longer and more complex.&lt;br /&gt;
&lt;br /&gt;
====Selecting Records from Multiple Tables====&lt;br /&gt;
&lt;br /&gt;
Using the JDatabaseQuery&#039;s [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#join join] methods, we can select records from multiple related tables. The generic &amp;quot;join&amp;quot; method takes two arguments; the join &amp;quot;type&amp;quot; (inner, outer, left, right) and the join condition. In the following example you will notice that we can use all of the keywords we would normally use if we were writing a native SQL query, including the AS keyword for aliasing tables and the ON keyword for creating relationships between tables. Also note that the table alias is used in all methods which reference table columns (I.e. select, where, order).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Select all articles for users who have a username which starts with &#039;a&#039;.&lt;br /&gt;
// Order it by the created date.&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;select(array(&#039;a.*&#039;, &#039;b.username&#039;, &#039;b.name&#039;))&lt;br /&gt;
    -&amp;gt;from(&#039;#__content AS a&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;INNER&#039;, &#039;#__users AS b ON (a.created_by = b.id)&#039;)&lt;br /&gt;
    -&amp;gt;where(&#039;b.username LIKE \&#039;a%\&#039;&#039;)&lt;br /&gt;
    -&amp;gt;order(&#039;a.created DESC&#039;);&lt;br /&gt;
&lt;br /&gt;
// Reset the query using our newly populated query object.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
// Load the results as a list of stdClass objects.&lt;br /&gt;
$results = $db-&amp;gt;loadObjectList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The join method above enables us to query both the content and user tables, retrieving articles with their author details. There are also convenience methods for [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#innerJoin inner], [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#leftJoin left], [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#rightJoin right] and [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#outerJoin outer] joins.&lt;br /&gt;
&lt;br /&gt;
We can use multiple joins to query across more than two tables:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;select(array(&#039;a.*&#039;, &#039;b.username&#039;, &#039;b.name&#039;, &#039;c.*&#039;, &#039;d.*&#039;))&lt;br /&gt;
    -&amp;gt;from(&#039;#__content AS a&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;INNER&#039;, &#039;#__users AS b ON (a.created_by = b.id)&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;LEFT&#039;, &#039;#__user_profiles AS c ON (b.id = c.user_id)&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;RIGHT&#039;, &#039;#__categories AS d ON (a.catid = d.id)&#039;)&lt;br /&gt;
    -&amp;gt;where(&#039;b.username LIKE \&#039;a%\&#039;&#039;)&lt;br /&gt;
    -&amp;gt;order(&#039;a.created DESC&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice how chaining makes the source code much more readable for these longer queries.&lt;br /&gt;
&lt;br /&gt;
====Inserting a Record====&lt;br /&gt;
&lt;br /&gt;
=====Using SQL=====&lt;br /&gt;
&lt;br /&gt;
The JDatabaseQuery class provides a number of methods for building insert queries, the most common being [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#insert insert], [http://api.joomla.org/11.4/Joomla-Platform/Database/JDatabaseQuery.html#columns columns] and [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#values values].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Insert columns.&lt;br /&gt;
$columns = array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;);&lt;br /&gt;
&lt;br /&gt;
// Insert values.&lt;br /&gt;
$values = array(1001, $db-&amp;gt;quote(&#039;custom.message&#039;), $db-&amp;gt;quote(&#039;Inserting a record using insert()&#039;), 1);&lt;br /&gt;
&lt;br /&gt;
// Prepare the insert query.&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;insert($db-&amp;gt;quoteName(&#039;#__user_profiles&#039;))&lt;br /&gt;
    -&amp;gt;columns($db-&amp;gt;quoteName($columns))&lt;br /&gt;
    -&amp;gt;values(implode(&#039;,&#039;, $values));&lt;br /&gt;
&lt;br /&gt;
// Reset the query using our newly populated query object.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since Joomla 3.0 there has been a change in how the Joomla database class executes a query.&lt;br /&gt;
&lt;br /&gt;
When executing a query in the Joomla Framework 11.4 we use the JDatabase&#039;s [http://api.joomla.org/11.4/Joomla-Platform/Database/JDatabase.html#query query] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
try {&lt;br /&gt;
    // Execute the query in Joomla 2.5.&lt;br /&gt;
    $result = $db-&amp;gt;query();&lt;br /&gt;
} catch (Exception $e) {&lt;br /&gt;
    // catch any database errors.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For Joomla Framework 12.1 and higher, we use the JDatabaseDriver&#039;s [http://api.joomla.org/Joomla-Platform/Database/JDatabaseDriver.html#execute execute] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
try {&lt;br /&gt;
    // Execute the query in Joomla 3.0.&lt;br /&gt;
    $result = $db-&amp;gt;execute();&lt;br /&gt;
} catch (Exception $e) {&lt;br /&gt;
    // catch any database errors.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since the phase out of PHP 4.x support, PHP&#039;s exception model has been widely implemented in the Joomla Framework. Therefore, we can wrap our database execution in a try...catch block to catch any unexpected database errors that may arise. Any database-specific errors will be thrown using an instance of the [http://api.joomla.org/Joomla-Platform/Database/JDatabaseException.html JDatabaseException] class although it is best practice to catch the generic Exception object from which JDatabaseException inherits.&lt;br /&gt;
&lt;br /&gt;
=====Using an Object=====&lt;br /&gt;
&lt;br /&gt;
The JDatabaseDriver also class provides us with a convenience method for saving an object directly to the database allowing us to add a record to a table without writing a single line of SQL.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Create and populate an object.&lt;br /&gt;
$profile = new stdClass();&lt;br /&gt;
$profile-&amp;gt;user_id = 1001;&lt;br /&gt;
$profile-&amp;gt;profile_key=&#039;custom.message&#039;;&lt;br /&gt;
$profile-&amp;gt;profile_value=&#039;Inserting a record using insertObject()&#039;;&lt;br /&gt;
$profile-&amp;gt;ordering=1;&lt;br /&gt;
&lt;br /&gt;
try {&lt;br /&gt;
    // Insert the object into the user profile table.&lt;br /&gt;
    $result = JFactory::getDbo()-&amp;gt;insertObject(&#039;#__user_profiles&#039;, $profile);&lt;br /&gt;
catch (Exception $e) {&lt;br /&gt;
    // catch any errors.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice here that we do not need to escape the table name; the insertObject method does this for us.&lt;br /&gt;
&lt;br /&gt;
The insertObject method will throw a [http://php.net/manual/en/class.runtimeexception.php RuntimeException] if there is a problem inserting the record into the database table.&lt;br /&gt;
&lt;br /&gt;
We need to ensure that the record does not exist before attempting to insert it, so adding some kind of record check before executing the insertObject method would be recommended.&lt;br /&gt;
&lt;br /&gt;
====Updating a Record====&lt;br /&gt;
&lt;br /&gt;
=====Using SQL=====&lt;br /&gt;
&lt;br /&gt;
The JDatabaseQuery class also provides methods for building update queries, in particular [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#update update] and [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#set set]. We also reuse another method which we used when creating select statements, the where method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Fields to update.&lt;br /&gt;
$fields = array(&lt;br /&gt;
    &#039;profile_value=\&#039;Updating custom message for user 1001.\&#039;&#039;,&lt;br /&gt;
    &#039;ordering=2&#039;);&lt;br /&gt;
&lt;br /&gt;
// Conditions for which records should be updated.&lt;br /&gt;
$conditions = array(&lt;br /&gt;
    &#039;user_id=42&#039;, &lt;br /&gt;
    &#039;profile_key=\&#039;custom.message\&#039;&#039;);&lt;br /&gt;
&lt;br /&gt;
$query-&amp;gt;update($db-&amp;gt;quoteName(&#039;#__user_profiles&#039;))-&amp;gt;set($fields)-&amp;gt;where($conditions);&lt;br /&gt;
&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
try {&lt;br /&gt;
    $result = $db-&amp;gt;query();&lt;br /&gt;
catch (Exception $e) {&lt;br /&gt;
    // Catch the error.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Using an Object =====&lt;br /&gt;
&lt;br /&gt;
Like insertObject, the JDatabaseDriver class provides a convenience method for updating an object.&lt;br /&gt;
&lt;br /&gt;
Below we will update our custom table with new values using an existing id primary key:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Create an object for the record we are going to update.&lt;br /&gt;
$object = new stdClass();&lt;br /&gt;
&lt;br /&gt;
// Must be a valid primary key value.&lt;br /&gt;
$object-&amp;gt;id = 1;&lt;br /&gt;
$object-&amp;gt;title = &#039;My Custom Record&#039;;&lt;br /&gt;
$object-&amp;gt;description = &#039;A custom record being updated in the database.&#039;;&lt;br /&gt;
&lt;br /&gt;
try {&lt;br /&gt;
    // Update their details in the users table using id as the primary key.&lt;br /&gt;
    $result = JFactory::getDbo()-&amp;gt;updateObject(&#039;#__custom_table&#039;, $object, &#039;id&#039;);&lt;br /&gt;
} catch (Exception $e) {&lt;br /&gt;
    // catch the error.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Just like insertObject, updateObject takes care of escaping table names for us.&lt;br /&gt;
&lt;br /&gt;
The updateObject method will throw a RuntimeException if there is a problem inserting the record into the database table.&lt;br /&gt;
&lt;br /&gt;
We need to ensure that the record already exists before attempting to update it, so we would probably add some kind of record check before executing the updateObject method.&lt;br /&gt;
&lt;br /&gt;
====Deleting a Record====&lt;br /&gt;
&lt;br /&gt;
Just as there are select, insert and update method calls, there is also a delete method for remove records from the database.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// delete all custom keys for user 1001.&lt;br /&gt;
$conditions = array(&lt;br /&gt;
    &#039;user_id=1001&#039;, &lt;br /&gt;
    &#039;profile_key LIKE \&#039;custom.%\&#039;&#039;);&lt;br /&gt;
&lt;br /&gt;
$query-&amp;gt;delete($db-&amp;gt;quoteName(&#039;#__user_profiles&#039;));&lt;br /&gt;
$query-&amp;gt;where($conditions);&lt;br /&gt;
&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
try {&lt;br /&gt;
   $result = $db-&amp;gt;query(); // $db-&amp;gt;execute(); for Joomla 3.0.&lt;br /&gt;
} catch (Exception $e) {&lt;br /&gt;
   // catch the error.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Haydenyoung</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J2.5:Accessing_the_database_using_JDatabase&amp;diff=77650</id>
		<title>J2.5:Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J2.5:Accessing_the_database_using_JDatabase&amp;diff=77650"/>
		<updated>2012-11-20T14:22:17Z</updated>

		<summary type="html">&lt;p&gt;Haydenyoung: /* Using updateObject */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{inuse}}&lt;br /&gt;
&lt;br /&gt;
Joomla provides a sophisticated database abstraction layer to simplify the usage for third party developers. New versions of the Joomla Platform API provide additional functionality which extends the database layer further, and includes features such as connectors to a greater variety of database servers and the query chaining to improve readability of connection code and simplify SQL coding.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
&lt;br /&gt;
Joomla can use different kinds of SQL database systems and run in a variety of environments with different table-prefixes. In addition to these functions, the class automatically creates the database connection. Besides instantiating the object you need just two lines of code to get a result from the database in a variety of formats. Using the Joomla database layer ensures a maximum of compatibility and flexibility for your extension.&lt;br /&gt;
&lt;br /&gt;
===The Connection===&lt;br /&gt;
&lt;br /&gt;
Joomla supports a generic interface for connecting to the database, hiding the intricacies of a specific database server from the Framework developer, thus making Joomla code easier to port from one platform to another.&lt;br /&gt;
&lt;br /&gt;
To obtain a database connection, use the [http://api.joomla.org/Joomla-Platform/JFactory.html#getDbo JFactory getDbo] static method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The getDbo method will return a reference to a high-level abstract class called [http://api.joomla.org/Joomla-Platform/Database/JDatabaseDriver.html JDatabaseDriver]; the Joomla Framework hides the specifics of the database server, which is loaded dynamically by getDbo via Joomla&#039;s configuration file.&lt;br /&gt;
&lt;br /&gt;
Alternatively, if your class inherits from the JModel (or a decendent class) getDbo method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = $this-&amp;gt;getDbo();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{JVer|3.0}} users should note that the JModel class has been renamed JModelLegacy and will eventually be deprecated in favour of the class JModelBase. Therefore, any Joomla 3.0 core or extension code should use JFactory::getDbo() to obtain a database connection.&lt;br /&gt;
&lt;br /&gt;
===The Query===&lt;br /&gt;
&lt;br /&gt;
Joomla&#039;s database querying has changed since the new Joomla Framework was introduced  &amp;quot;query chaining&amp;quot; is now the recommended method for building database queries (although string queries are still supported).&lt;br /&gt;
&lt;br /&gt;
Query chaining refers to a method of connecting a number of methods, one after the other, with each method returning an object that can support the next method, improving readability and simplifying code.&lt;br /&gt;
&lt;br /&gt;
To obtain a new instance of the JDatabaseQuery class we use the JDatabaseDriver getQuery method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The JDatabaseDriver::getQuery takes an optional argument, $new, which can be true or false (the default being false).&lt;br /&gt;
&lt;br /&gt;
To query our data source we can call a number of JDatabaseQuery methods; these methods encapsulate the data source&#039;s query language (in most cases SQL), hiding query-specific syntax from the developer and increasing the portability of the developer&#039;s source code. &lt;br /&gt;
&lt;br /&gt;
Some of the more frequently used methods include; select, from, join, where and order. There are also methods such as insert, update and delete for modifying records in the data store. By chaining these and other method calls, you can create almost any query against your data store without compromising portability of your code.&lt;br /&gt;
&lt;br /&gt;
====Selecting Records from a Single Table====&lt;br /&gt;
&lt;br /&gt;
Below is an example of creating a database query using the JDatabaseQuery class. Using the select, from, where and order methods, we can create queries which are flexible, easily readable and portable:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Select all records from the user profile table where key begins with &amp;quot;custom.&amp;quot;.&lt;br /&gt;
// Order it by the ordering field.&lt;br /&gt;
$query-&amp;gt;select(array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;));&lt;br /&gt;
$query-&amp;gt;from(&#039;#__user_profiles&#039;);&lt;br /&gt;
$query-&amp;gt;where(&#039;profile_key LIKE \&#039;custom.%\&#039;&#039;);&lt;br /&gt;
$query-&amp;gt;order(&#039;ordering ASC&#039;);&lt;br /&gt;
&lt;br /&gt;
// Reset the query using our newly populated query object.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
// Load the results as a list of stdClass objects.&lt;br /&gt;
$results = $db-&amp;gt;loadObjectList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The query can also be chained to simplify further:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;select(array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;))&lt;br /&gt;
    -&amp;gt;from(&#039;#__user_profiles&#039;)&lt;br /&gt;
    -&amp;gt;where(&#039;profile_key LIKE \&#039;custom.%\&#039;&#039;)&lt;br /&gt;
    -&amp;gt;order(&#039;ordering ASC&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Chaining can become useful when queries become longer and more complex.&lt;br /&gt;
&lt;br /&gt;
====Selecting Records from Multiple Tables====&lt;br /&gt;
&lt;br /&gt;
Using the JDatabaseQuery&#039;s [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#join join] methods, we can select records from multiple related tables. The generic &amp;quot;join&amp;quot; method takes two arguments; the join &amp;quot;type&amp;quot; (inner, outer, left, right) and the join condition. In the following example you will notice that we can use all of the keywords we would normally use if we were writing a native SQL query, including the AS keyword for aliasing tables and the ON keyword for creating relationships between tables. Also note that the table alias is used in all methods which reference table columns (I.e. select, where, order).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Select all articles for users who have a username which starts with &#039;a&#039;.&lt;br /&gt;
// Order it by the created date.&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;select(array(&#039;a.*&#039;, &#039;b.username&#039;, &#039;b.name&#039;))&lt;br /&gt;
    -&amp;gt;from(&#039;#__content AS a&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;INNER&#039;, &#039;#__users AS b ON (a.created_by = b.id)&#039;)&lt;br /&gt;
    -&amp;gt;where(&#039;b.username LIKE \&#039;a%\&#039;&#039;)&lt;br /&gt;
    -&amp;gt;order(&#039;a.created DESC&#039;);&lt;br /&gt;
&lt;br /&gt;
// Reset the query using our newly populated query object.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
// Load the results as a list of stdClass objects.&lt;br /&gt;
$results = $db-&amp;gt;loadObjectList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The join method above enables us to query both the content and user tables, retrieving articles with their author details. There are also convenience methods for [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#innerJoin inner], [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#leftJoin left], [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#rightJoin right] and [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#outerJoin outer] joins.&lt;br /&gt;
&lt;br /&gt;
We can use multiple joins to query across more than two tables:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;select(array(&#039;a.*&#039;, &#039;b.username&#039;, &#039;b.name&#039;, &#039;c.*&#039;, &#039;d.*&#039;))&lt;br /&gt;
    -&amp;gt;from(&#039;#__content AS a&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;INNER&#039;, &#039;#__users AS b ON (a.created_by = b.id)&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;LEFT&#039;, &#039;#__user_profiles AS c ON (b.id = c.user_id)&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;RIGHT&#039;, &#039;#__categories AS d ON (a.catid = d.id)&#039;)&lt;br /&gt;
    -&amp;gt;where(&#039;b.username LIKE \&#039;a%\&#039;&#039;)&lt;br /&gt;
    -&amp;gt;order(&#039;a.created DESC&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice how chaining makes the source code much more readable for these longer queries.&lt;br /&gt;
&lt;br /&gt;
====Inserting a Record====&lt;br /&gt;
&lt;br /&gt;
=====Using SQL=====&lt;br /&gt;
&lt;br /&gt;
The JDatabaseQuery class provides a number of methods for building insert queries, the most common being [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#insert insert], [http://api.joomla.org/11.4/Joomla-Platform/Database/JDatabaseQuery.html#columns columns] and [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#values values].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Insert columns.&lt;br /&gt;
$columns = array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;);&lt;br /&gt;
&lt;br /&gt;
// Insert values.&lt;br /&gt;
$values = array(1001, $db-&amp;gt;quote(&#039;custom.message&#039;), $db-&amp;gt;quote(&#039;Inserting a record using insert()&#039;), 1);&lt;br /&gt;
&lt;br /&gt;
// Prepare the insert query.&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;insert($db-&amp;gt;quoteName(&#039;#__user_profiles&#039;))&lt;br /&gt;
    -&amp;gt;columns($db-&amp;gt;quoteName($columns))&lt;br /&gt;
    -&amp;gt;values(implode(&#039;,&#039;, $values));&lt;br /&gt;
&lt;br /&gt;
// Reset the query using our newly populated query object.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since Joomla 3.0 there has been a change in how the Joomla database class executes a query.&lt;br /&gt;
&lt;br /&gt;
When executing a query in the Joomla Framework 11.4 we use the JDatabase&#039;s [http://api.joomla.org/11.4/Joomla-Platform/Database/JDatabase.html#query query] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
try {&lt;br /&gt;
    // Execute the query in Joomla 2.5.&lt;br /&gt;
    $result = $db-&amp;gt;query();&lt;br /&gt;
} catch (Exception $e) {&lt;br /&gt;
    // catch any database errors.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For Joomla Framework 12.1 and higher, we use the JDatabaseDriver&#039;s [http://api.joomla.org/Joomla-Platform/Database/JDatabaseDriver.html#execute execute] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
try {&lt;br /&gt;
    // Execute the query in Joomla 3.0.&lt;br /&gt;
    $result = $db-&amp;gt;execute();&lt;br /&gt;
} catch (Exception $e) {&lt;br /&gt;
    // catch any database errors.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since the phase out of PHP 4.x support, PHP&#039;s exception model has been widely implemented in the Joomla Framework. Therefore, we can wrap our database execution in a try...catch block to catch any unexpected database errors that may arise. Any database-specific errors will be thrown using an instance of the [http://api.joomla.org/Joomla-Platform/Database/JDatabaseException.html JDatabaseException] class although it is best practice to catch the generic Exception object from which JDatabaseException inherits.&lt;br /&gt;
&lt;br /&gt;
=====Using an Object=====&lt;br /&gt;
&lt;br /&gt;
The JDatabaseDriver also class provides us with a convenience method for saving an object directly to the database allowing us to add a record to a table without writing a single line of SQL.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Create and populate an object.&lt;br /&gt;
$profile = new stdClass();&lt;br /&gt;
$profile-&amp;gt;user_id = 1001;&lt;br /&gt;
$profile-&amp;gt;profile_key=&#039;custom.message&#039;;&lt;br /&gt;
$profile-&amp;gt;profile_value=&#039;Inserting a record using insertObject()&#039;;&lt;br /&gt;
$profile-&amp;gt;ordering=1;&lt;br /&gt;
&lt;br /&gt;
try {&lt;br /&gt;
    // Insert the object into the user profile table.&lt;br /&gt;
    $result = JFactory::getDbo()-&amp;gt;insertObject(&#039;#__user_profiles&#039;, $profile);&lt;br /&gt;
catch (Exception $e) {&lt;br /&gt;
    // catch any errors.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice here that we do not need to escape the table name; the insertObject method does this for us.&lt;br /&gt;
&lt;br /&gt;
The insertObject method will throw a [http://php.net/manual/en/class.runtimeexception.php RuntimeException] if there is a problem inserting the record into the database table.&lt;br /&gt;
&lt;br /&gt;
We need to ensure that the record does not exist before attempting to insert it, so adding some kind of record check before executing the insertObject method would be recommended.&lt;br /&gt;
&lt;br /&gt;
====Updating a Record====&lt;br /&gt;
&lt;br /&gt;
=====Using SQL=====&lt;br /&gt;
&lt;br /&gt;
The JDatabaseQuery class also provides methods for building update queries, in particular [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#update update] and [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#set set]. We also reuse another method which we used when creating select statements, the where method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Fields to update.&lt;br /&gt;
$fields = array(&lt;br /&gt;
    &#039;profile_value=\&#039;Updating custom message for user 1001.\&#039;&#039;,&lt;br /&gt;
    &#039;ordering=2&#039;);&lt;br /&gt;
&lt;br /&gt;
// Conditions for which records should be updated.&lt;br /&gt;
$conditions = array(&lt;br /&gt;
    &#039;user_id=42&#039;, &lt;br /&gt;
    &#039;profile_key=\&#039;custom.message\&#039;&#039;);&lt;br /&gt;
&lt;br /&gt;
$query-&amp;gt;update($db-&amp;gt;quoteName(&#039;#__user_profiles&#039;))-&amp;gt;set($fields)-&amp;gt;where($conditions);&lt;br /&gt;
&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
try {&lt;br /&gt;
    $result = $db-&amp;gt;query();&lt;br /&gt;
catch (Exception $e) {&lt;br /&gt;
    // Catch the error.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Using an Object =====&lt;br /&gt;
&lt;br /&gt;
Like insertObject, the JDatabaseDriver class provides a convenience method for updating an object.&lt;br /&gt;
&lt;br /&gt;
Below we will update our custom table with new values using an existing id primary key:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Create an object for the record we are going to update.&lt;br /&gt;
$object = new stdClass();&lt;br /&gt;
&lt;br /&gt;
// Must be a valid primary key value.&lt;br /&gt;
$object-&amp;gt;id = 1;&lt;br /&gt;
$object-&amp;gt;title = &#039;My Custom Record&#039;;&lt;br /&gt;
$object-&amp;gt;description = &#039;A custom record being updated in the database.&#039;;&lt;br /&gt;
&lt;br /&gt;
try {&lt;br /&gt;
    // Update their details in the users table using id as the primary key.&lt;br /&gt;
    $result = JFactory::getDbo()-&amp;gt;updateObject(&#039;#__custom_table&#039;, $object, &#039;id&#039;);&lt;br /&gt;
} catch (Exception $e) {&lt;br /&gt;
    // catch the error.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Just like insertObject, updateObject takes care of escaping table names for us.&lt;br /&gt;
&lt;br /&gt;
The updateObject method will throw a RuntimeException if there is a problem inserting the record into the database table.&lt;br /&gt;
&lt;br /&gt;
We need to ensure that the record already exists before attempting to update it, so we would probably add some kind of record check before executing the updateObject method.&lt;br /&gt;
&lt;br /&gt;
====Deleting a Record====&lt;/div&gt;</summary>
		<author><name>Haydenyoung</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J2.5:Accessing_the_database_using_JDatabase&amp;diff=77649</id>
		<title>J2.5:Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J2.5:Accessing_the_database_using_JDatabase&amp;diff=77649"/>
		<updated>2012-11-20T14:20:56Z</updated>

		<summary type="html">&lt;p&gt;Haydenyoung: /* Using an Object */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{inuse}}&lt;br /&gt;
&lt;br /&gt;
Joomla provides a sophisticated database abstraction layer to simplify the usage for third party developers. New versions of the Joomla Platform API provide additional functionality which extends the database layer further, and includes features such as connectors to a greater variety of database servers and the query chaining to improve readability of connection code and simplify SQL coding.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
&lt;br /&gt;
Joomla can use different kinds of SQL database systems and run in a variety of environments with different table-prefixes. In addition to these functions, the class automatically creates the database connection. Besides instantiating the object you need just two lines of code to get a result from the database in a variety of formats. Using the Joomla database layer ensures a maximum of compatibility and flexibility for your extension.&lt;br /&gt;
&lt;br /&gt;
===The Connection===&lt;br /&gt;
&lt;br /&gt;
Joomla supports a generic interface for connecting to the database, hiding the intricacies of a specific database server from the Framework developer, thus making Joomla code easier to port from one platform to another.&lt;br /&gt;
&lt;br /&gt;
To obtain a database connection, use the [http://api.joomla.org/Joomla-Platform/JFactory.html#getDbo JFactory getDbo] static method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The getDbo method will return a reference to a high-level abstract class called [http://api.joomla.org/Joomla-Platform/Database/JDatabaseDriver.html JDatabaseDriver]; the Joomla Framework hides the specifics of the database server, which is loaded dynamically by getDbo via Joomla&#039;s configuration file.&lt;br /&gt;
&lt;br /&gt;
Alternatively, if your class inherits from the JModel (or a decendent class) getDbo method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = $this-&amp;gt;getDbo();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{JVer|3.0}} users should note that the JModel class has been renamed JModelLegacy and will eventually be deprecated in favour of the class JModelBase. Therefore, any Joomla 3.0 core or extension code should use JFactory::getDbo() to obtain a database connection.&lt;br /&gt;
&lt;br /&gt;
===The Query===&lt;br /&gt;
&lt;br /&gt;
Joomla&#039;s database querying has changed since the new Joomla Framework was introduced  &amp;quot;query chaining&amp;quot; is now the recommended method for building database queries (although string queries are still supported).&lt;br /&gt;
&lt;br /&gt;
Query chaining refers to a method of connecting a number of methods, one after the other, with each method returning an object that can support the next method, improving readability and simplifying code.&lt;br /&gt;
&lt;br /&gt;
To obtain a new instance of the JDatabaseQuery class we use the JDatabaseDriver getQuery method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The JDatabaseDriver::getQuery takes an optional argument, $new, which can be true or false (the default being false).&lt;br /&gt;
&lt;br /&gt;
To query our data source we can call a number of JDatabaseQuery methods; these methods encapsulate the data source&#039;s query language (in most cases SQL), hiding query-specific syntax from the developer and increasing the portability of the developer&#039;s source code. &lt;br /&gt;
&lt;br /&gt;
Some of the more frequently used methods include; select, from, join, where and order. There are also methods such as insert, update and delete for modifying records in the data store. By chaining these and other method calls, you can create almost any query against your data store without compromising portability of your code.&lt;br /&gt;
&lt;br /&gt;
====Selecting Records from a Single Table====&lt;br /&gt;
&lt;br /&gt;
Below is an example of creating a database query using the JDatabaseQuery class. Using the select, from, where and order methods, we can create queries which are flexible, easily readable and portable:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Select all records from the user profile table where key begins with &amp;quot;custom.&amp;quot;.&lt;br /&gt;
// Order it by the ordering field.&lt;br /&gt;
$query-&amp;gt;select(array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;));&lt;br /&gt;
$query-&amp;gt;from(&#039;#__user_profiles&#039;);&lt;br /&gt;
$query-&amp;gt;where(&#039;profile_key LIKE \&#039;custom.%\&#039;&#039;);&lt;br /&gt;
$query-&amp;gt;order(&#039;ordering ASC&#039;);&lt;br /&gt;
&lt;br /&gt;
// Reset the query using our newly populated query object.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
// Load the results as a list of stdClass objects.&lt;br /&gt;
$results = $db-&amp;gt;loadObjectList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The query can also be chained to simplify further:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;select(array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;))&lt;br /&gt;
    -&amp;gt;from(&#039;#__user_profiles&#039;)&lt;br /&gt;
    -&amp;gt;where(&#039;profile_key LIKE \&#039;custom.%\&#039;&#039;)&lt;br /&gt;
    -&amp;gt;order(&#039;ordering ASC&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Chaining can become useful when queries become longer and more complex.&lt;br /&gt;
&lt;br /&gt;
====Selecting Records from Multiple Tables====&lt;br /&gt;
&lt;br /&gt;
Using the JDatabaseQuery&#039;s [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#join join] methods, we can select records from multiple related tables. The generic &amp;quot;join&amp;quot; method takes two arguments; the join &amp;quot;type&amp;quot; (inner, outer, left, right) and the join condition. In the following example you will notice that we can use all of the keywords we would normally use if we were writing a native SQL query, including the AS keyword for aliasing tables and the ON keyword for creating relationships between tables. Also note that the table alias is used in all methods which reference table columns (I.e. select, where, order).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Select all articles for users who have a username which starts with &#039;a&#039;.&lt;br /&gt;
// Order it by the created date.&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;select(array(&#039;a.*&#039;, &#039;b.username&#039;, &#039;b.name&#039;))&lt;br /&gt;
    -&amp;gt;from(&#039;#__content AS a&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;INNER&#039;, &#039;#__users AS b ON (a.created_by = b.id)&#039;)&lt;br /&gt;
    -&amp;gt;where(&#039;b.username LIKE \&#039;a%\&#039;&#039;)&lt;br /&gt;
    -&amp;gt;order(&#039;a.created DESC&#039;);&lt;br /&gt;
&lt;br /&gt;
// Reset the query using our newly populated query object.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
// Load the results as a list of stdClass objects.&lt;br /&gt;
$results = $db-&amp;gt;loadObjectList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The join method above enables us to query both the content and user tables, retrieving articles with their author details. There are also convenience methods for [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#innerJoin inner], [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#leftJoin left], [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#rightJoin right] and [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#outerJoin outer] joins.&lt;br /&gt;
&lt;br /&gt;
We can use multiple joins to query across more than two tables:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;select(array(&#039;a.*&#039;, &#039;b.username&#039;, &#039;b.name&#039;, &#039;c.*&#039;, &#039;d.*&#039;))&lt;br /&gt;
    -&amp;gt;from(&#039;#__content AS a&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;INNER&#039;, &#039;#__users AS b ON (a.created_by = b.id)&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;LEFT&#039;, &#039;#__user_profiles AS c ON (b.id = c.user_id)&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;RIGHT&#039;, &#039;#__categories AS d ON (a.catid = d.id)&#039;)&lt;br /&gt;
    -&amp;gt;where(&#039;b.username LIKE \&#039;a%\&#039;&#039;)&lt;br /&gt;
    -&amp;gt;order(&#039;a.created DESC&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice how chaining makes the source code much more readable for these longer queries.&lt;br /&gt;
&lt;br /&gt;
====Inserting a Record====&lt;br /&gt;
&lt;br /&gt;
=====Using SQL=====&lt;br /&gt;
&lt;br /&gt;
The JDatabaseQuery class provides a number of methods for building insert queries, the most common being [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#insert insert], [http://api.joomla.org/11.4/Joomla-Platform/Database/JDatabaseQuery.html#columns columns] and [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#values values].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Insert columns.&lt;br /&gt;
$columns = array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;);&lt;br /&gt;
&lt;br /&gt;
// Insert values.&lt;br /&gt;
$values = array(1001, $db-&amp;gt;quote(&#039;custom.message&#039;), $db-&amp;gt;quote(&#039;Inserting a record using insert()&#039;), 1);&lt;br /&gt;
&lt;br /&gt;
// Prepare the insert query.&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;insert($db-&amp;gt;quoteName(&#039;#__user_profiles&#039;))&lt;br /&gt;
    -&amp;gt;columns($db-&amp;gt;quoteName($columns))&lt;br /&gt;
    -&amp;gt;values(implode(&#039;,&#039;, $values));&lt;br /&gt;
&lt;br /&gt;
// Reset the query using our newly populated query object.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since Joomla 3.0 there has been a change in how the Joomla database class executes a query.&lt;br /&gt;
&lt;br /&gt;
When executing a query in the Joomla Framework 11.4 we use the JDatabase&#039;s [http://api.joomla.org/11.4/Joomla-Platform/Database/JDatabase.html#query query] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
try {&lt;br /&gt;
    // Execute the query in Joomla 2.5.&lt;br /&gt;
    $result = $db-&amp;gt;query();&lt;br /&gt;
} catch (Exception $e) {&lt;br /&gt;
    // catch any database errors.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For Joomla Framework 12.1 and higher, we use the JDatabaseDriver&#039;s [http://api.joomla.org/Joomla-Platform/Database/JDatabaseDriver.html#execute execute] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
try {&lt;br /&gt;
    // Execute the query in Joomla 3.0.&lt;br /&gt;
    $result = $db-&amp;gt;execute();&lt;br /&gt;
} catch (Exception $e) {&lt;br /&gt;
    // catch any database errors.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since the phase out of PHP 4.x support, PHP&#039;s exception model has been widely implemented in the Joomla Framework. Therefore, we can wrap our database execution in a try...catch block to catch any unexpected database errors that may arise. Any database-specific errors will be thrown using an instance of the [http://api.joomla.org/Joomla-Platform/Database/JDatabaseException.html JDatabaseException] class although it is best practice to catch the generic Exception object from which JDatabaseException inherits.&lt;br /&gt;
&lt;br /&gt;
=====Using an Object=====&lt;br /&gt;
&lt;br /&gt;
The JDatabaseDriver also class provides us with a convenience method for saving an object directly to the database allowing us to add a record to a table without writing a single line of SQL.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Create and populate an object.&lt;br /&gt;
$profile = new stdClass();&lt;br /&gt;
$profile-&amp;gt;user_id = 1001;&lt;br /&gt;
$profile-&amp;gt;profile_key=&#039;custom.message&#039;;&lt;br /&gt;
$profile-&amp;gt;profile_value=&#039;Inserting a record using insertObject()&#039;;&lt;br /&gt;
$profile-&amp;gt;ordering=1;&lt;br /&gt;
&lt;br /&gt;
try {&lt;br /&gt;
    // Insert the object into the user profile table.&lt;br /&gt;
    $result = JFactory::getDbo()-&amp;gt;insertObject(&#039;#__user_profiles&#039;, $profile);&lt;br /&gt;
catch (Exception $e) {&lt;br /&gt;
    // catch any errors.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice here that we do not need to escape the table name; the insertObject method does this for us.&lt;br /&gt;
&lt;br /&gt;
The insertObject method will throw a [http://php.net/manual/en/class.runtimeexception.php RuntimeException] if there is a problem inserting the record into the database table.&lt;br /&gt;
&lt;br /&gt;
We need to ensure that the record does not exist before attempting to insert it, so adding some kind of record check before executing the insertObject method would be recommended.&lt;br /&gt;
&lt;br /&gt;
====Updating a Record====&lt;br /&gt;
&lt;br /&gt;
=====Using SQL=====&lt;br /&gt;
&lt;br /&gt;
The JDatabaseQuery class also provides methods for building update queries, in particular [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#update update] and [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#set set]. We also reuse another method which we used when creating select statements, the where method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Fields to update.&lt;br /&gt;
$fields = array(&lt;br /&gt;
    &#039;profile_value=\&#039;Updating custom message for user 1001.\&#039;&#039;,&lt;br /&gt;
    &#039;ordering=2&#039;);&lt;br /&gt;
&lt;br /&gt;
// Conditions for which records should be updated.&lt;br /&gt;
$conditions = array(&lt;br /&gt;
    &#039;user_id=42&#039;, &lt;br /&gt;
    &#039;profile_key=\&#039;custom.message\&#039;&#039;);&lt;br /&gt;
&lt;br /&gt;
$query-&amp;gt;update($db-&amp;gt;quoteName(&#039;#__user_profiles&#039;))-&amp;gt;set($fields)-&amp;gt;where($conditions);&lt;br /&gt;
&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
try {&lt;br /&gt;
    $result = $db-&amp;gt;query();&lt;br /&gt;
catch (Exception $e) {&lt;br /&gt;
    // Catch the error.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Using updateObject=====&lt;br /&gt;
&lt;br /&gt;
Like insertObject, the JDatabaseDriver class provides a convenience method for updating an object.&lt;br /&gt;
&lt;br /&gt;
Below we will update our custom table with new values using an existing id primary key:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Create an object for the record we are going to update.&lt;br /&gt;
$object = new stdClass();&lt;br /&gt;
&lt;br /&gt;
// Must be a valid primary key value.&lt;br /&gt;
$object-&amp;gt;id = 1;&lt;br /&gt;
$object-&amp;gt;title = &#039;My Custom Record&#039;;&lt;br /&gt;
$object-&amp;gt;description = &#039;A custom record being updated in the database.&#039;;&lt;br /&gt;
&lt;br /&gt;
try {&lt;br /&gt;
    // Update their details in the users table using id as the primary key.&lt;br /&gt;
    $result = JFactory::getDbo()-&amp;gt;updateObject(&#039;#__custom_table&#039;, $object, &#039;id&#039;);&lt;br /&gt;
} catch (Exception $e) {&lt;br /&gt;
    // catch the error.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We need to ensure that the record already exists before attempting to update it, so we would probably add some kind of record check before executing the updateObject method.&lt;br /&gt;
&lt;br /&gt;
====Deleting a Record====&lt;/div&gt;</summary>
		<author><name>Haydenyoung</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J2.5:Accessing_the_database_using_JDatabase&amp;diff=77648</id>
		<title>J2.5:Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J2.5:Accessing_the_database_using_JDatabase&amp;diff=77648"/>
		<updated>2012-11-20T14:19:51Z</updated>

		<summary type="html">&lt;p&gt;Haydenyoung: /* Updating a Record */ Updating a record using an object.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{inuse}}&lt;br /&gt;
&lt;br /&gt;
Joomla provides a sophisticated database abstraction layer to simplify the usage for third party developers. New versions of the Joomla Platform API provide additional functionality which extends the database layer further, and includes features such as connectors to a greater variety of database servers and the query chaining to improve readability of connection code and simplify SQL coding.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
&lt;br /&gt;
Joomla can use different kinds of SQL database systems and run in a variety of environments with different table-prefixes. In addition to these functions, the class automatically creates the database connection. Besides instantiating the object you need just two lines of code to get a result from the database in a variety of formats. Using the Joomla database layer ensures a maximum of compatibility and flexibility for your extension.&lt;br /&gt;
&lt;br /&gt;
===The Connection===&lt;br /&gt;
&lt;br /&gt;
Joomla supports a generic interface for connecting to the database, hiding the intricacies of a specific database server from the Framework developer, thus making Joomla code easier to port from one platform to another.&lt;br /&gt;
&lt;br /&gt;
To obtain a database connection, use the [http://api.joomla.org/Joomla-Platform/JFactory.html#getDbo JFactory getDbo] static method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The getDbo method will return a reference to a high-level abstract class called [http://api.joomla.org/Joomla-Platform/Database/JDatabaseDriver.html JDatabaseDriver]; the Joomla Framework hides the specifics of the database server, which is loaded dynamically by getDbo via Joomla&#039;s configuration file.&lt;br /&gt;
&lt;br /&gt;
Alternatively, if your class inherits from the JModel (or a decendent class) getDbo method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = $this-&amp;gt;getDbo();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{JVer|3.0}} users should note that the JModel class has been renamed JModelLegacy and will eventually be deprecated in favour of the class JModelBase. Therefore, any Joomla 3.0 core or extension code should use JFactory::getDbo() to obtain a database connection.&lt;br /&gt;
&lt;br /&gt;
===The Query===&lt;br /&gt;
&lt;br /&gt;
Joomla&#039;s database querying has changed since the new Joomla Framework was introduced  &amp;quot;query chaining&amp;quot; is now the recommended method for building database queries (although string queries are still supported).&lt;br /&gt;
&lt;br /&gt;
Query chaining refers to a method of connecting a number of methods, one after the other, with each method returning an object that can support the next method, improving readability and simplifying code.&lt;br /&gt;
&lt;br /&gt;
To obtain a new instance of the JDatabaseQuery class we use the JDatabaseDriver getQuery method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The JDatabaseDriver::getQuery takes an optional argument, $new, which can be true or false (the default being false).&lt;br /&gt;
&lt;br /&gt;
To query our data source we can call a number of JDatabaseQuery methods; these methods encapsulate the data source&#039;s query language (in most cases SQL), hiding query-specific syntax from the developer and increasing the portability of the developer&#039;s source code. &lt;br /&gt;
&lt;br /&gt;
Some of the more frequently used methods include; select, from, join, where and order. There are also methods such as insert, update and delete for modifying records in the data store. By chaining these and other method calls, you can create almost any query against your data store without compromising portability of your code.&lt;br /&gt;
&lt;br /&gt;
====Selecting Records from a Single Table====&lt;br /&gt;
&lt;br /&gt;
Below is an example of creating a database query using the JDatabaseQuery class. Using the select, from, where and order methods, we can create queries which are flexible, easily readable and portable:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Select all records from the user profile table where key begins with &amp;quot;custom.&amp;quot;.&lt;br /&gt;
// Order it by the ordering field.&lt;br /&gt;
$query-&amp;gt;select(array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;));&lt;br /&gt;
$query-&amp;gt;from(&#039;#__user_profiles&#039;);&lt;br /&gt;
$query-&amp;gt;where(&#039;profile_key LIKE \&#039;custom.%\&#039;&#039;);&lt;br /&gt;
$query-&amp;gt;order(&#039;ordering ASC&#039;);&lt;br /&gt;
&lt;br /&gt;
// Reset the query using our newly populated query object.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
// Load the results as a list of stdClass objects.&lt;br /&gt;
$results = $db-&amp;gt;loadObjectList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The query can also be chained to simplify further:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;select(array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;))&lt;br /&gt;
    -&amp;gt;from(&#039;#__user_profiles&#039;)&lt;br /&gt;
    -&amp;gt;where(&#039;profile_key LIKE \&#039;custom.%\&#039;&#039;)&lt;br /&gt;
    -&amp;gt;order(&#039;ordering ASC&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Chaining can become useful when queries become longer and more complex.&lt;br /&gt;
&lt;br /&gt;
====Selecting Records from Multiple Tables====&lt;br /&gt;
&lt;br /&gt;
Using the JDatabaseQuery&#039;s [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#join join] methods, we can select records from multiple related tables. The generic &amp;quot;join&amp;quot; method takes two arguments; the join &amp;quot;type&amp;quot; (inner, outer, left, right) and the join condition. In the following example you will notice that we can use all of the keywords we would normally use if we were writing a native SQL query, including the AS keyword for aliasing tables and the ON keyword for creating relationships between tables. Also note that the table alias is used in all methods which reference table columns (I.e. select, where, order).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Select all articles for users who have a username which starts with &#039;a&#039;.&lt;br /&gt;
// Order it by the created date.&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;select(array(&#039;a.*&#039;, &#039;b.username&#039;, &#039;b.name&#039;))&lt;br /&gt;
    -&amp;gt;from(&#039;#__content AS a&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;INNER&#039;, &#039;#__users AS b ON (a.created_by = b.id)&#039;)&lt;br /&gt;
    -&amp;gt;where(&#039;b.username LIKE \&#039;a%\&#039;&#039;)&lt;br /&gt;
    -&amp;gt;order(&#039;a.created DESC&#039;);&lt;br /&gt;
&lt;br /&gt;
// Reset the query using our newly populated query object.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
// Load the results as a list of stdClass objects.&lt;br /&gt;
$results = $db-&amp;gt;loadObjectList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The join method above enables us to query both the content and user tables, retrieving articles with their author details. There are also convenience methods for [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#innerJoin inner], [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#leftJoin left], [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#rightJoin right] and [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#outerJoin outer] joins.&lt;br /&gt;
&lt;br /&gt;
We can use multiple joins to query across more than two tables:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;select(array(&#039;a.*&#039;, &#039;b.username&#039;, &#039;b.name&#039;, &#039;c.*&#039;, &#039;d.*&#039;))&lt;br /&gt;
    -&amp;gt;from(&#039;#__content AS a&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;INNER&#039;, &#039;#__users AS b ON (a.created_by = b.id)&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;LEFT&#039;, &#039;#__user_profiles AS c ON (b.id = c.user_id)&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;RIGHT&#039;, &#039;#__categories AS d ON (a.catid = d.id)&#039;)&lt;br /&gt;
    -&amp;gt;where(&#039;b.username LIKE \&#039;a%\&#039;&#039;)&lt;br /&gt;
    -&amp;gt;order(&#039;a.created DESC&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice how chaining makes the source code much more readable for these longer queries.&lt;br /&gt;
&lt;br /&gt;
====Inserting a Record====&lt;br /&gt;
&lt;br /&gt;
=====Using SQL=====&lt;br /&gt;
&lt;br /&gt;
The JDatabaseQuery class provides a number of methods for building insert queries, the most common being [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#insert insert], [http://api.joomla.org/11.4/Joomla-Platform/Database/JDatabaseQuery.html#columns columns] and [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#values values].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Insert columns.&lt;br /&gt;
$columns = array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;);&lt;br /&gt;
&lt;br /&gt;
// Insert values.&lt;br /&gt;
$values = array(1001, $db-&amp;gt;quote(&#039;custom.message&#039;), $db-&amp;gt;quote(&#039;Inserting a record using insert()&#039;), 1);&lt;br /&gt;
&lt;br /&gt;
// Prepare the insert query.&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;insert($db-&amp;gt;quoteName(&#039;#__user_profiles&#039;))&lt;br /&gt;
    -&amp;gt;columns($db-&amp;gt;quoteName($columns))&lt;br /&gt;
    -&amp;gt;values(implode(&#039;,&#039;, $values));&lt;br /&gt;
&lt;br /&gt;
// Reset the query using our newly populated query object.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since Joomla 3.0 there has been a change in how the Joomla database class executes a query.&lt;br /&gt;
&lt;br /&gt;
When executing a query in the Joomla Framework 11.4 we use the JDatabase&#039;s [http://api.joomla.org/11.4/Joomla-Platform/Database/JDatabase.html#query query] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
try {&lt;br /&gt;
    // Execute the query in Joomla 2.5.&lt;br /&gt;
    $result = $db-&amp;gt;query();&lt;br /&gt;
} catch (Exception $e) {&lt;br /&gt;
    // catch any database errors.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For Joomla Framework 12.1 and higher, we use the JDatabaseDriver&#039;s [http://api.joomla.org/Joomla-Platform/Database/JDatabaseDriver.html#execute execute] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
try {&lt;br /&gt;
    // Execute the query in Joomla 3.0.&lt;br /&gt;
    $result = $db-&amp;gt;execute();&lt;br /&gt;
} catch (Exception $e) {&lt;br /&gt;
    // catch any database errors.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since the phase out of PHP 4.x support, PHP&#039;s exception model has been widely implemented in the Joomla Framework. Therefore, we can wrap our database execution in a try...catch block to catch any unexpected database errors that may arise. Any database-specific errors will be thrown using an instance of the [http://api.joomla.org/Joomla-Platform/Database/JDatabaseException.html JDatabaseException] class although it is best practice to catch the generic Exception object from which JDatabaseException inherits.&lt;br /&gt;
&lt;br /&gt;
=====Using an Object=====&lt;br /&gt;
&lt;br /&gt;
The JDatabaseDriver also class provides us with a convenience method for saving an object directly to the database allowing us to add a record to a table without writing a single line of SQL.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Create and populate an object.&lt;br /&gt;
$profile = new stdClass();&lt;br /&gt;
$profile-&amp;gt;user_id = 1001;&lt;br /&gt;
$profile-&amp;gt;profile_key=&#039;custom.message&#039;;&lt;br /&gt;
$profile-&amp;gt;profile_value=&#039;Inserting a record using insertObject()&#039;;&lt;br /&gt;
$profile-&amp;gt;ordering=1;&lt;br /&gt;
&lt;br /&gt;
try {&lt;br /&gt;
    // Insert the object into the user profile table.&lt;br /&gt;
    $result = JFactory::getDbo()-&amp;gt;insertObject(&#039;#__user_profiles&#039;, $profile);&lt;br /&gt;
catch (Exception $e) {&lt;br /&gt;
    // catch any errors.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice here that we do not need to escape the table name; the insertObject method does this for us.&lt;br /&gt;
&lt;br /&gt;
The insertObject method will throw a [http://php.net/manual/en/class.runtimeexception.php RuntimeException] if there is a problem inserting the record into the database table.&lt;br /&gt;
&lt;br /&gt;
====Updating a Record====&lt;br /&gt;
&lt;br /&gt;
=====Using SQL=====&lt;br /&gt;
&lt;br /&gt;
The JDatabaseQuery class also provides methods for building update queries, in particular [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#update update] and [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#set set]. We also reuse another method which we used when creating select statements, the where method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Fields to update.&lt;br /&gt;
$fields = array(&lt;br /&gt;
    &#039;profile_value=\&#039;Updating custom message for user 1001.\&#039;&#039;,&lt;br /&gt;
    &#039;ordering=2&#039;);&lt;br /&gt;
&lt;br /&gt;
// Conditions for which records should be updated.&lt;br /&gt;
$conditions = array(&lt;br /&gt;
    &#039;user_id=42&#039;, &lt;br /&gt;
    &#039;profile_key=\&#039;custom.message\&#039;&#039;);&lt;br /&gt;
&lt;br /&gt;
$query-&amp;gt;update($db-&amp;gt;quoteName(&#039;#__user_profiles&#039;))-&amp;gt;set($fields)-&amp;gt;where($conditions);&lt;br /&gt;
&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
try {&lt;br /&gt;
    $result = $db-&amp;gt;query();&lt;br /&gt;
catch (Exception $e) {&lt;br /&gt;
    // Catch the error.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Using updateObject=====&lt;br /&gt;
&lt;br /&gt;
Like insertObject, the JDatabaseDriver class provides a convenience method for updating an object.&lt;br /&gt;
&lt;br /&gt;
Below we will update our custom table with new values using an existing id primary key:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Create an object for the record we are going to update.&lt;br /&gt;
$object = new stdClass();&lt;br /&gt;
&lt;br /&gt;
// Must be a valid primary key value.&lt;br /&gt;
$object-&amp;gt;id = 1;&lt;br /&gt;
$object-&amp;gt;title = &#039;My Custom Record&#039;;&lt;br /&gt;
$object-&amp;gt;description = &#039;A custom record being updated in the database.&#039;;&lt;br /&gt;
&lt;br /&gt;
try {&lt;br /&gt;
    // Update their details in the users table using id as the primary key.&lt;br /&gt;
    $result = JFactory::getDbo()-&amp;gt;updateObject(&#039;#__custom_table&#039;, $object, &#039;id&#039;);&lt;br /&gt;
} catch (Exception $e) {&lt;br /&gt;
    // catch the error.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We need to ensure that the record already exists before attempting to update it, so we would probably add some kind of record check before executing the updateObject method.&lt;br /&gt;
&lt;br /&gt;
====Deleting a Record====&lt;/div&gt;</summary>
		<author><name>Haydenyoung</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J2.5:Accessing_the_database_using_JDatabase&amp;diff=77645</id>
		<title>J2.5:Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J2.5:Accessing_the_database_using_JDatabase&amp;diff=77645"/>
		<updated>2012-11-20T10:54:24Z</updated>

		<summary type="html">&lt;p&gt;Haydenyoung: /* Updating a Record */ Updating a record.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{inuse}}&lt;br /&gt;
&lt;br /&gt;
Joomla provides a sophisticated database abstraction layer to simplify the usage for third party developers. New versions of the Joomla Platform API provide additional functionality which extends the database layer further, and includes features such as connectors to a greater variety of database servers and the query chaining to improve readability of connection code and simplify SQL coding.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
&lt;br /&gt;
Joomla can use different kinds of SQL database systems and run in a variety of environments with different table-prefixes. In addition to these functions, the class automatically creates the database connection. Besides instantiating the object you need just two lines of code to get a result from the database in a variety of formats. Using the Joomla database layer ensures a maximum of compatibility and flexibility for your extension.&lt;br /&gt;
&lt;br /&gt;
===The Connection===&lt;br /&gt;
&lt;br /&gt;
Joomla supports a generic interface for connecting to the database, hiding the intricacies of a specific database server from the Framework developer, thus making Joomla code easier to port from one platform to another.&lt;br /&gt;
&lt;br /&gt;
To obtain a database connection, use the [http://api.joomla.org/Joomla-Platform/JFactory.html#getDbo JFactory getDbo] static method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The getDbo method will return a reference to a high-level abstract class called [http://api.joomla.org/Joomla-Platform/Database/JDatabaseDriver.html JDatabaseDriver]; the Joomla Framework hides the specifics of the database server, which is loaded dynamically by getDbo via Joomla&#039;s configuration file.&lt;br /&gt;
&lt;br /&gt;
Alternatively, if your class inherits from the JModel (or a decendent class) getDbo method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = $this-&amp;gt;getDbo();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{JVer|3.0}} users should note that the JModel class has been renamed JModelLegacy and will eventually be deprecated in favour of the class JModelBase. Therefore, any Joomla 3.0 core or extension code should use JFactory::getDbo() to obtain a database connection.&lt;br /&gt;
&lt;br /&gt;
===The Query===&lt;br /&gt;
&lt;br /&gt;
Joomla&#039;s database querying has changed since the new Joomla Framework was introduced  &amp;quot;query chaining&amp;quot; is now the recommended method for building database queries (although string queries are still supported).&lt;br /&gt;
&lt;br /&gt;
Query chaining refers to a method of connecting a number of methods, one after the other, with each method returning an object that can support the next method, improving readability and simplifying code.&lt;br /&gt;
&lt;br /&gt;
To obtain a new instance of the JDatabaseQuery class we use the JDatabaseDriver getQuery method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The JDatabaseDriver::getQuery takes an optional argument, $new, which can be true or false (the default being false).&lt;br /&gt;
&lt;br /&gt;
To query our data source we can call a number of JDatabaseQuery methods; these methods encapsulate the data source&#039;s query language (in most cases SQL), hiding query-specific syntax from the developer and increasing the portability of the developer&#039;s source code. &lt;br /&gt;
&lt;br /&gt;
Some of the more frequently used methods include; select, from, join, where and order. There are also methods such as insert, update and delete for modifying records in the data store. By chaining these and other method calls, you can create almost any query against your data store without compromising portability of your code.&lt;br /&gt;
&lt;br /&gt;
====Selecting Records from a Single Table====&lt;br /&gt;
&lt;br /&gt;
Below is an example of creating a database query using the JDatabaseQuery class. Using the select, from, where and order methods, we can create queries which are flexible, easily readable and portable:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Select all records from the user profile table where key begins with &amp;quot;custom.&amp;quot;.&lt;br /&gt;
// Order it by the ordering field.&lt;br /&gt;
$query-&amp;gt;select(array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;));&lt;br /&gt;
$query-&amp;gt;from(&#039;#__user_profiles&#039;);&lt;br /&gt;
$query-&amp;gt;where(&#039;profile_key LIKE \&#039;custom.%\&#039;&#039;);&lt;br /&gt;
$query-&amp;gt;order(&#039;ordering ASC&#039;);&lt;br /&gt;
&lt;br /&gt;
// Reset the query using our newly populated query object.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
// Load the results as a list of stdClass objects.&lt;br /&gt;
$results = $db-&amp;gt;loadObjectList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The query can also be chained to simplify further:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;select(array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;))&lt;br /&gt;
    -&amp;gt;from(&#039;#__user_profiles&#039;)&lt;br /&gt;
    -&amp;gt;where(&#039;profile_key LIKE \&#039;custom.%\&#039;&#039;)&lt;br /&gt;
    -&amp;gt;order(&#039;ordering ASC&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Chaining can become useful when queries become longer and more complex.&lt;br /&gt;
&lt;br /&gt;
====Selecting Records from Multiple Tables====&lt;br /&gt;
&lt;br /&gt;
Using the JDatabaseQuery&#039;s [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#join join] methods, we can select records from multiple related tables. The generic &amp;quot;join&amp;quot; method takes two arguments; the join &amp;quot;type&amp;quot; (inner, outer, left, right) and the join condition. In the following example you will notice that we can use all of the keywords we would normally use if we were writing a native SQL query, including the AS keyword for aliasing tables and the ON keyword for creating relationships between tables. Also note that the table alias is used in all methods which reference table columns (I.e. select, where, order).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Select all articles for users who have a username which starts with &#039;a&#039;.&lt;br /&gt;
// Order it by the created date.&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;select(array(&#039;a.*&#039;, &#039;b.username&#039;, &#039;b.name&#039;))&lt;br /&gt;
    -&amp;gt;from(&#039;#__content AS a&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;INNER&#039;, &#039;#__users AS b ON (a.created_by = b.id)&#039;)&lt;br /&gt;
    -&amp;gt;where(&#039;b.username LIKE \&#039;a%\&#039;&#039;)&lt;br /&gt;
    -&amp;gt;order(&#039;a.created DESC&#039;);&lt;br /&gt;
&lt;br /&gt;
// Reset the query using our newly populated query object.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
// Load the results as a list of stdClass objects.&lt;br /&gt;
$results = $db-&amp;gt;loadObjectList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The join method above enables us to query both the content and user tables, retrieving articles with their author details. There are also convenience methods for [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#innerJoin inner], [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#leftJoin left], [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#rightJoin right] and [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#outerJoin outer] joins.&lt;br /&gt;
&lt;br /&gt;
We can use multiple joins to query across more than two tables:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;select(array(&#039;a.*&#039;, &#039;b.username&#039;, &#039;b.name&#039;, &#039;c.*&#039;, &#039;d.*&#039;))&lt;br /&gt;
    -&amp;gt;from(&#039;#__content AS a&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;INNER&#039;, &#039;#__users AS b ON (a.created_by = b.id)&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;LEFT&#039;, &#039;#__user_profiles AS c ON (b.id = c.user_id)&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;RIGHT&#039;, &#039;#__categories AS d ON (a.catid = d.id)&#039;)&lt;br /&gt;
    -&amp;gt;where(&#039;b.username LIKE \&#039;a%\&#039;&#039;)&lt;br /&gt;
    -&amp;gt;order(&#039;a.created DESC&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice how chaining makes the source code much more readable for these longer queries.&lt;br /&gt;
&lt;br /&gt;
====Inserting a Record====&lt;br /&gt;
&lt;br /&gt;
=====Using SQL=====&lt;br /&gt;
&lt;br /&gt;
The JDatabaseQuery class provides a number of methods for building insert queries, the most common being [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#insert insert], [http://api.joomla.org/11.4/Joomla-Platform/Database/JDatabaseQuery.html#columns columns] and [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#values values].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Insert columns.&lt;br /&gt;
$columns = array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;);&lt;br /&gt;
&lt;br /&gt;
// Insert values.&lt;br /&gt;
$values = array(1001, $db-&amp;gt;quote(&#039;custom.message&#039;), $db-&amp;gt;quote(&#039;Inserting a record using insert()&#039;), 1);&lt;br /&gt;
&lt;br /&gt;
// Prepare the insert query.&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;insert($db-&amp;gt;quoteName(&#039;#__user_profiles&#039;))&lt;br /&gt;
    -&amp;gt;columns($db-&amp;gt;quoteName($columns))&lt;br /&gt;
    -&amp;gt;values(implode(&#039;,&#039;, $values));&lt;br /&gt;
&lt;br /&gt;
// Reset the query using our newly populated query object.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since Joomla 3.0 there has been a change in how the Joomla database class executes a query.&lt;br /&gt;
&lt;br /&gt;
When executing a query in the Joomla Framework 11.4 we use the JDatabase&#039;s [http://api.joomla.org/11.4/Joomla-Platform/Database/JDatabase.html#query query] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
try {&lt;br /&gt;
    // Execute the query in Joomla 2.5.&lt;br /&gt;
    $result = $db-&amp;gt;query();&lt;br /&gt;
} catch (Exception $e) {&lt;br /&gt;
    // catch any database errors.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For Joomla Framework 12.1 and higher, we use the JDatabaseDriver&#039;s [http://api.joomla.org/Joomla-Platform/Database/JDatabaseDriver.html#execute execute] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
try {&lt;br /&gt;
    // Execute the query in Joomla 3.0.&lt;br /&gt;
    $result = $db-&amp;gt;execute();&lt;br /&gt;
} catch (Exception $e) {&lt;br /&gt;
    // catch any database errors.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since the phase out of PHP 4.x support, PHP&#039;s exception model has been widely implemented in the Joomla Framework. Therefore, we can wrap our database execution in a try...catch block to catch any unexpected database errors that may arise. Any database-specific errors will be thrown using an instance of the [http://api.joomla.org/Joomla-Platform/Database/JDatabaseException.html JDatabaseException] class although it is best practice to catch the generic Exception object from which JDatabaseException inherits.&lt;br /&gt;
&lt;br /&gt;
=====Using an Object=====&lt;br /&gt;
&lt;br /&gt;
The JDatabaseDriver also class provides us with a convenience method for saving an object directly to the database allowing us to add a record to a table without writing a single line of SQL.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Create and populate an object.&lt;br /&gt;
$profile = new stdClass();&lt;br /&gt;
$profile-&amp;gt;user_id = 1001;&lt;br /&gt;
$profile-&amp;gt;profile_key=&#039;custom.message&#039;;&lt;br /&gt;
$profile-&amp;gt;profile_value=&#039;Inserting a record using insertObject()&#039;;&lt;br /&gt;
$profile-&amp;gt;ordering=1;&lt;br /&gt;
&lt;br /&gt;
try {&lt;br /&gt;
    // Insert the object into the user profile table.&lt;br /&gt;
    $result = JFactory::getDbo()-&amp;gt;insertObject(&#039;#__user_profiles&#039;, $profile);&lt;br /&gt;
catch (Exception $e) {&lt;br /&gt;
    // catch any errors.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice here that we do not need to escape the table name; the insertObject method does this for us.&lt;br /&gt;
&lt;br /&gt;
The insertObject method will throw a [http://php.net/manual/en/class.runtimeexception.php RuntimeException] if there is a problem inserting the record into the database table.&lt;br /&gt;
&lt;br /&gt;
====Updating a Record====&lt;br /&gt;
&lt;br /&gt;
The JDatabaseQuery class also provides methods for building update queries, in particular [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#update update] and [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#set set]. We also reuse another method which we used when creating select statements, the where method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Fields to update.&lt;br /&gt;
$fields = array(&lt;br /&gt;
    &#039;profile_value=\&#039;Updating custom message for user 1001.\&#039;&#039;,&lt;br /&gt;
    &#039;ordering=2&#039;);&lt;br /&gt;
&lt;br /&gt;
// Conditions for which records should be updated.&lt;br /&gt;
$conditions = array(&lt;br /&gt;
    &#039;user_id=42&#039;, &lt;br /&gt;
    &#039;profile_key=\&#039;custom.message\&#039;&#039;);&lt;br /&gt;
&lt;br /&gt;
$query-&amp;gt;update($db-&amp;gt;quoteName(&#039;#__user_profiles&#039;))-&amp;gt;set($fields)-&amp;gt;where($conditions);&lt;br /&gt;
&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
try {&lt;br /&gt;
    $result = $db-&amp;gt;query();&lt;br /&gt;
catch (Exception $e) {&lt;br /&gt;
    // Catch the error.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Deleting a Record====&lt;/div&gt;</summary>
		<author><name>Haydenyoung</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J2.5:Accessing_the_database_using_JDatabase&amp;diff=77643</id>
		<title>J2.5:Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J2.5:Accessing_the_database_using_JDatabase&amp;diff=77643"/>
		<updated>2012-11-20T08:43:33Z</updated>

		<summary type="html">&lt;p&gt;Haydenyoung: /* Using an Object */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{inuse}}&lt;br /&gt;
&lt;br /&gt;
Joomla provides a sophisticated database abstraction layer to simplify the usage for third party developers. New versions of the Joomla Platform API provide additional functionality which extends the database layer further, and includes features such as connectors to a greater variety of database servers and the query chaining to improve readability of connection code and simplify SQL coding.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
&lt;br /&gt;
Joomla can use different kinds of SQL database systems and run in a variety of environments with different table-prefixes. In addition to these functions, the class automatically creates the database connection. Besides instantiating the object you need just two lines of code to get a result from the database in a variety of formats. Using the Joomla database layer ensures a maximum of compatibility and flexibility for your extension.&lt;br /&gt;
&lt;br /&gt;
===The Connection===&lt;br /&gt;
&lt;br /&gt;
Joomla supports a generic interface for connecting to the database, hiding the intricacies of a specific database server from the Framework developer, thus making Joomla code easier to port from one platform to another.&lt;br /&gt;
&lt;br /&gt;
To obtain a database connection, use the [http://api.joomla.org/Joomla-Platform/JFactory.html#getDbo JFactory getDbo] static method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The getDbo method will return a reference to a high-level abstract class called [http://api.joomla.org/Joomla-Platform/Database/JDatabaseDriver.html JDatabaseDriver]; the Joomla Framework hides the specifics of the database server, which is loaded dynamically by getDbo via Joomla&#039;s configuration file.&lt;br /&gt;
&lt;br /&gt;
Alternatively, if your class inherits from the JModel (or a decendent class) getDbo method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = $this-&amp;gt;getDbo();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{JVer|3.0}} users should note that the JModel class has been renamed JModelLegacy and will eventually be deprecated in favour of the class JModelBase. Therefore, any Joomla 3.0 core or extension code should use JFactory::getDbo() to obtain a database connection.&lt;br /&gt;
&lt;br /&gt;
===The Query===&lt;br /&gt;
&lt;br /&gt;
Joomla&#039;s database querying has changed since the new Joomla Framework was introduced  &amp;quot;query chaining&amp;quot; is now the recommended method for building database queries (although string queries are still supported).&lt;br /&gt;
&lt;br /&gt;
Query chaining refers to a method of connecting a number of methods, one after the other, with each method returning an object that can support the next method, improving readability and simplifying code.&lt;br /&gt;
&lt;br /&gt;
To obtain a new instance of the JDatabaseQuery class we use the JDatabaseDriver getQuery method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The JDatabaseDriver::getQuery takes an optional argument, $new, which can be true or false (the default being false).&lt;br /&gt;
&lt;br /&gt;
To query our data source we can call a number of JDatabaseQuery methods; these methods encapsulate the data source&#039;s query language (in most cases SQL), hiding query-specific syntax from the developer and increasing the portability of the developer&#039;s source code. &lt;br /&gt;
&lt;br /&gt;
Some of the more frequently used methods include; select, from, join, where and order. There are also methods such as insert, update and delete for modifying records in the data store. By chaining these and other method calls, you can create almost any query against your data store without compromising portability of your code.&lt;br /&gt;
&lt;br /&gt;
====Selecting Records from a Single Table====&lt;br /&gt;
&lt;br /&gt;
Below is an example of creating a database query using the JDatabaseQuery class. Using the select, from, where and order methods, we can create queries which are flexible, easily readable and portable:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Select all records from the user profile table where key begins with &amp;quot;custom.&amp;quot;.&lt;br /&gt;
// Order it by the ordering field.&lt;br /&gt;
$query-&amp;gt;select(array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;));&lt;br /&gt;
$query-&amp;gt;from(&#039;#__user_profiles&#039;);&lt;br /&gt;
$query-&amp;gt;where(&#039;profile_key LIKE \&#039;custom.%\&#039;&#039;);&lt;br /&gt;
$query-&amp;gt;order(&#039;ordering ASC&#039;);&lt;br /&gt;
&lt;br /&gt;
// Reset the query using our newly populated query object.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
// Load the results as a list of stdClass objects.&lt;br /&gt;
$results = $db-&amp;gt;loadObjectList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The query can also be chained to simplify further:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;select(array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;))&lt;br /&gt;
    -&amp;gt;from(&#039;#__user_profiles&#039;)&lt;br /&gt;
    -&amp;gt;where(&#039;profile_key LIKE \&#039;custom.%\&#039;&#039;)&lt;br /&gt;
    -&amp;gt;order(&#039;ordering ASC&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Chaining can become useful when queries become longer and more complex.&lt;br /&gt;
&lt;br /&gt;
====Selecting Records from Multiple Tables====&lt;br /&gt;
&lt;br /&gt;
Using the JDatabaseQuery&#039;s [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#join join] methods, we can select records from multiple related tables. The generic &amp;quot;join&amp;quot; method takes two arguments; the join &amp;quot;type&amp;quot; (inner, outer, left, right) and the join condition. In the following example you will notice that we can use all of the keywords we would normally use if we were writing a native SQL query, including the AS keyword for aliasing tables and the ON keyword for creating relationships between tables. Also note that the table alias is used in all methods which reference table columns (I.e. select, where, order).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Select all articles for users who have a username which starts with &#039;a&#039;.&lt;br /&gt;
// Order it by the created date.&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;select(array(&#039;a.*&#039;, &#039;b.username&#039;, &#039;b.name&#039;))&lt;br /&gt;
    -&amp;gt;from(&#039;#__content AS a&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;INNER&#039;, &#039;#__users AS b ON (a.created_by = b.id)&#039;)&lt;br /&gt;
    -&amp;gt;where(&#039;b.username LIKE \&#039;a%\&#039;&#039;)&lt;br /&gt;
    -&amp;gt;order(&#039;a.created DESC&#039;);&lt;br /&gt;
&lt;br /&gt;
// Reset the query using our newly populated query object.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
// Load the results as a list of stdClass objects.&lt;br /&gt;
$results = $db-&amp;gt;loadObjectList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The join method above enables us to query both the content and user tables, retrieving articles with their author details. There are also convenience methods for [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#innerJoin inner], [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#leftJoin left], [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#rightJoin right] and [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#outerJoin outer] joins.&lt;br /&gt;
&lt;br /&gt;
We can use multiple joins to query across more than two tables:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;select(array(&#039;a.*&#039;, &#039;b.username&#039;, &#039;b.name&#039;, &#039;c.*&#039;, &#039;d.*&#039;))&lt;br /&gt;
    -&amp;gt;from(&#039;#__content AS a&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;INNER&#039;, &#039;#__users AS b ON (a.created_by = b.id)&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;LEFT&#039;, &#039;#__user_profiles AS c ON (b.id = c.user_id)&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;RIGHT&#039;, &#039;#__categories AS d ON (a.catid = d.id)&#039;)&lt;br /&gt;
    -&amp;gt;where(&#039;b.username LIKE \&#039;a%\&#039;&#039;)&lt;br /&gt;
    -&amp;gt;order(&#039;a.created DESC&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice how chaining makes the source code much more readable for these longer queries.&lt;br /&gt;
&lt;br /&gt;
====Inserting a Record====&lt;br /&gt;
&lt;br /&gt;
=====Using SQL=====&lt;br /&gt;
&lt;br /&gt;
The JDatabaseQuery class provides a number of methods for building insert queries, the most common being [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#insert insert], [http://api.joomla.org/11.4/Joomla-Platform/Database/JDatabaseQuery.html#columns columns] and [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#values values].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Insert columns.&lt;br /&gt;
$columns = array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;);&lt;br /&gt;
&lt;br /&gt;
// Insert values.&lt;br /&gt;
$values = array(1001, $db-&amp;gt;quote(&#039;custom.message&#039;), $db-&amp;gt;quote(&#039;Inserting a record using insert()&#039;), 1);&lt;br /&gt;
&lt;br /&gt;
// Prepare the insert query.&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;insert($db-&amp;gt;quoteName(&#039;#__user_profiles&#039;))&lt;br /&gt;
    -&amp;gt;columns($db-&amp;gt;quoteName($columns))&lt;br /&gt;
    -&amp;gt;values(implode(&#039;,&#039;, $values));&lt;br /&gt;
&lt;br /&gt;
// Reset the query using our newly populated query object.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since Joomla 3.0 there has been a change in how the Joomla database class executes a query.&lt;br /&gt;
&lt;br /&gt;
When executing a query in the Joomla Framework 11.4 we use the JDatabase&#039;s [http://api.joomla.org/11.4/Joomla-Platform/Database/JDatabase.html#query query] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
try {&lt;br /&gt;
    // Execute the query in Joomla 2.5.&lt;br /&gt;
    $result = $db-&amp;gt;query();&lt;br /&gt;
} catch (Exception $e) {&lt;br /&gt;
    // catch any database errors.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For Joomla Framework 12.1 and higher, we use the JDatabaseDriver&#039;s [http://api.joomla.org/Joomla-Platform/Database/JDatabaseDriver.html#execute execute] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
try {&lt;br /&gt;
    // Execute the query in Joomla 3.0.&lt;br /&gt;
    $result = $db-&amp;gt;execute();&lt;br /&gt;
} catch (Exception $e) {&lt;br /&gt;
    // catch any database errors.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since the phase out of PHP 4.x support, PHP&#039;s exception model has been widely implemented in the Joomla Framework. Therefore, we can wrap our database execution in a try...catch block to catch any unexpected database errors that may arise. Any database-specific errors will be thrown using an instance of the [http://api.joomla.org/Joomla-Platform/Database/JDatabaseException.html JDatabaseException] class although it is best practice to catch the generic Exception object from which JDatabaseException inherits.&lt;br /&gt;
&lt;br /&gt;
=====Using an Object=====&lt;br /&gt;
&lt;br /&gt;
The JDatabaseDriver also class provides us with a convenience method for saving an object directly to the database allowing us to add a record to a table without writing a single line of SQL.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Create and populate an object.&lt;br /&gt;
$profile = new stdClass();&lt;br /&gt;
$profile-&amp;gt;user_id = 1001;&lt;br /&gt;
$profile-&amp;gt;profile_key=&#039;custom.message&#039;;&lt;br /&gt;
$profile-&amp;gt;profile_value=&#039;Inserting a record using insertObject()&#039;;&lt;br /&gt;
$profile-&amp;gt;ordering=1;&lt;br /&gt;
&lt;br /&gt;
try {&lt;br /&gt;
    // Insert the object into the user profile table.&lt;br /&gt;
    $result = JFactory::getDbo()-&amp;gt;insertObject(&#039;#__user_profiles&#039;, $profile);&lt;br /&gt;
catch (Exception $e) {&lt;br /&gt;
    // catch any errors.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice here that we do not need to escape the table name; the insertObject method does this for us.&lt;br /&gt;
&lt;br /&gt;
The insertObject method will throw a [http://php.net/manual/en/class.runtimeexception.php RuntimeException] if there is a problem inserting the record into the database table.&lt;br /&gt;
&lt;br /&gt;
====Updating a Record====&lt;br /&gt;
&lt;br /&gt;
====Deleting a Record====&lt;/div&gt;</summary>
		<author><name>Haydenyoung</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J2.5:Accessing_the_database_using_JDatabase&amp;diff=77642</id>
		<title>J2.5:Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J2.5:Accessing_the_database_using_JDatabase&amp;diff=77642"/>
		<updated>2012-11-20T08:37:35Z</updated>

		<summary type="html">&lt;p&gt;Haydenyoung: /* Using an Object */ Added commenting.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{inuse}}&lt;br /&gt;
&lt;br /&gt;
Joomla provides a sophisticated database abstraction layer to simplify the usage for third party developers. New versions of the Joomla Platform API provide additional functionality which extends the database layer further, and includes features such as connectors to a greater variety of database servers and the query chaining to improve readability of connection code and simplify SQL coding.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
&lt;br /&gt;
Joomla can use different kinds of SQL database systems and run in a variety of environments with different table-prefixes. In addition to these functions, the class automatically creates the database connection. Besides instantiating the object you need just two lines of code to get a result from the database in a variety of formats. Using the Joomla database layer ensures a maximum of compatibility and flexibility for your extension.&lt;br /&gt;
&lt;br /&gt;
===The Connection===&lt;br /&gt;
&lt;br /&gt;
Joomla supports a generic interface for connecting to the database, hiding the intricacies of a specific database server from the Framework developer, thus making Joomla code easier to port from one platform to another.&lt;br /&gt;
&lt;br /&gt;
To obtain a database connection, use the [http://api.joomla.org/Joomla-Platform/JFactory.html#getDbo JFactory getDbo] static method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The getDbo method will return a reference to a high-level abstract class called [http://api.joomla.org/Joomla-Platform/Database/JDatabaseDriver.html JDatabaseDriver]; the Joomla Framework hides the specifics of the database server, which is loaded dynamically by getDbo via Joomla&#039;s configuration file.&lt;br /&gt;
&lt;br /&gt;
Alternatively, if your class inherits from the JModel (or a decendent class) getDbo method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = $this-&amp;gt;getDbo();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{JVer|3.0}} users should note that the JModel class has been renamed JModelLegacy and will eventually be deprecated in favour of the class JModelBase. Therefore, any Joomla 3.0 core or extension code should use JFactory::getDbo() to obtain a database connection.&lt;br /&gt;
&lt;br /&gt;
===The Query===&lt;br /&gt;
&lt;br /&gt;
Joomla&#039;s database querying has changed since the new Joomla Framework was introduced  &amp;quot;query chaining&amp;quot; is now the recommended method for building database queries (although string queries are still supported).&lt;br /&gt;
&lt;br /&gt;
Query chaining refers to a method of connecting a number of methods, one after the other, with each method returning an object that can support the next method, improving readability and simplifying code.&lt;br /&gt;
&lt;br /&gt;
To obtain a new instance of the JDatabaseQuery class we use the JDatabaseDriver getQuery method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The JDatabaseDriver::getQuery takes an optional argument, $new, which can be true or false (the default being false).&lt;br /&gt;
&lt;br /&gt;
To query our data source we can call a number of JDatabaseQuery methods; these methods encapsulate the data source&#039;s query language (in most cases SQL), hiding query-specific syntax from the developer and increasing the portability of the developer&#039;s source code. &lt;br /&gt;
&lt;br /&gt;
Some of the more frequently used methods include; select, from, join, where and order. There are also methods such as insert, update and delete for modifying records in the data store. By chaining these and other method calls, you can create almost any query against your data store without compromising portability of your code.&lt;br /&gt;
&lt;br /&gt;
====Selecting Records from a Single Table====&lt;br /&gt;
&lt;br /&gt;
Below is an example of creating a database query using the JDatabaseQuery class. Using the select, from, where and order methods, we can create queries which are flexible, easily readable and portable:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Select all records from the user profile table where key begins with &amp;quot;custom.&amp;quot;.&lt;br /&gt;
// Order it by the ordering field.&lt;br /&gt;
$query-&amp;gt;select(array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;));&lt;br /&gt;
$query-&amp;gt;from(&#039;#__user_profiles&#039;);&lt;br /&gt;
$query-&amp;gt;where(&#039;profile_key LIKE \&#039;custom.%\&#039;&#039;);&lt;br /&gt;
$query-&amp;gt;order(&#039;ordering ASC&#039;);&lt;br /&gt;
&lt;br /&gt;
// Reset the query using our newly populated query object.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
// Load the results as a list of stdClass objects.&lt;br /&gt;
$results = $db-&amp;gt;loadObjectList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The query can also be chained to simplify further:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;select(array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;))&lt;br /&gt;
    -&amp;gt;from(&#039;#__user_profiles&#039;)&lt;br /&gt;
    -&amp;gt;where(&#039;profile_key LIKE \&#039;custom.%\&#039;&#039;)&lt;br /&gt;
    -&amp;gt;order(&#039;ordering ASC&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Chaining can become useful when queries become longer and more complex.&lt;br /&gt;
&lt;br /&gt;
====Selecting Records from Multiple Tables====&lt;br /&gt;
&lt;br /&gt;
Using the JDatabaseQuery&#039;s [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#join join] methods, we can select records from multiple related tables. The generic &amp;quot;join&amp;quot; method takes two arguments; the join &amp;quot;type&amp;quot; (inner, outer, left, right) and the join condition. In the following example you will notice that we can use all of the keywords we would normally use if we were writing a native SQL query, including the AS keyword for aliasing tables and the ON keyword for creating relationships between tables. Also note that the table alias is used in all methods which reference table columns (I.e. select, where, order).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Select all articles for users who have a username which starts with &#039;a&#039;.&lt;br /&gt;
// Order it by the created date.&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;select(array(&#039;a.*&#039;, &#039;b.username&#039;, &#039;b.name&#039;))&lt;br /&gt;
    -&amp;gt;from(&#039;#__content AS a&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;INNER&#039;, &#039;#__users AS b ON (a.created_by = b.id)&#039;)&lt;br /&gt;
    -&amp;gt;where(&#039;b.username LIKE \&#039;a%\&#039;&#039;)&lt;br /&gt;
    -&amp;gt;order(&#039;a.created DESC&#039;);&lt;br /&gt;
&lt;br /&gt;
// Reset the query using our newly populated query object.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
// Load the results as a list of stdClass objects.&lt;br /&gt;
$results = $db-&amp;gt;loadObjectList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The join method above enables us to query both the content and user tables, retrieving articles with their author details. There are also convenience methods for [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#innerJoin inner], [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#leftJoin left], [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#rightJoin right] and [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#outerJoin outer] joins.&lt;br /&gt;
&lt;br /&gt;
We can use multiple joins to query across more than two tables:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;select(array(&#039;a.*&#039;, &#039;b.username&#039;, &#039;b.name&#039;, &#039;c.*&#039;, &#039;d.*&#039;))&lt;br /&gt;
    -&amp;gt;from(&#039;#__content AS a&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;INNER&#039;, &#039;#__users AS b ON (a.created_by = b.id)&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;LEFT&#039;, &#039;#__user_profiles AS c ON (b.id = c.user_id)&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;RIGHT&#039;, &#039;#__categories AS d ON (a.catid = d.id)&#039;)&lt;br /&gt;
    -&amp;gt;where(&#039;b.username LIKE \&#039;a%\&#039;&#039;)&lt;br /&gt;
    -&amp;gt;order(&#039;a.created DESC&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice how chaining makes the source code much more readable for these longer queries.&lt;br /&gt;
&lt;br /&gt;
====Inserting a Record====&lt;br /&gt;
&lt;br /&gt;
=====Using SQL=====&lt;br /&gt;
&lt;br /&gt;
The JDatabaseQuery class provides a number of methods for building insert queries, the most common being [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#insert insert], [http://api.joomla.org/11.4/Joomla-Platform/Database/JDatabaseQuery.html#columns columns] and [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#values values].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Insert columns.&lt;br /&gt;
$columns = array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;);&lt;br /&gt;
&lt;br /&gt;
// Insert values.&lt;br /&gt;
$values = array(1001, $db-&amp;gt;quote(&#039;custom.message&#039;), $db-&amp;gt;quote(&#039;Inserting a record using insert()&#039;), 1);&lt;br /&gt;
&lt;br /&gt;
// Prepare the insert query.&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;insert($db-&amp;gt;quoteName(&#039;#__user_profiles&#039;))&lt;br /&gt;
    -&amp;gt;columns($db-&amp;gt;quoteName($columns))&lt;br /&gt;
    -&amp;gt;values(implode(&#039;,&#039;, $values));&lt;br /&gt;
&lt;br /&gt;
// Reset the query using our newly populated query object.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since Joomla 3.0 there has been a change in how the Joomla database class executes a query.&lt;br /&gt;
&lt;br /&gt;
When executing a query in the Joomla Framework 11.4 we use the JDatabase&#039;s [http://api.joomla.org/11.4/Joomla-Platform/Database/JDatabase.html#query query] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
try {&lt;br /&gt;
    // Execute the query in Joomla 2.5.&lt;br /&gt;
    $result = $db-&amp;gt;query();&lt;br /&gt;
} catch (Exception $e) {&lt;br /&gt;
    // catch any database errors.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For Joomla Framework 12.1 and higher, we use the JDatabaseDriver&#039;s [http://api.joomla.org/Joomla-Platform/Database/JDatabaseDriver.html#execute execute] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
try {&lt;br /&gt;
    // Execute the query in Joomla 3.0.&lt;br /&gt;
    $result = $db-&amp;gt;execute();&lt;br /&gt;
} catch (Exception $e) {&lt;br /&gt;
    // catch any database errors.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since the phase out of PHP 4.x support, PHP&#039;s exception model has been widely implemented in the Joomla Framework. Therefore, we can wrap our database execution in a try...catch block to catch any unexpected database errors that may arise. Any database-specific errors will be thrown using an instance of the [http://api.joomla.org/Joomla-Platform/Database/JDatabaseException.html JDatabaseException] class although it is best practice to catch the generic Exception object from which JDatabaseException inherits.&lt;br /&gt;
&lt;br /&gt;
=====Using an Object=====&lt;br /&gt;
&lt;br /&gt;
The JDatabaseDriver also class provides us with a convenience method for saving an object directly to the database allowing us to add a record to a table without writing a single line of SQL.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Create and populate an object.&lt;br /&gt;
$profile = new stdClass();&lt;br /&gt;
$profile-&amp;gt;user_id = 42;&lt;br /&gt;
$profile-&amp;gt;profile_key=&#039;custom.message&#039;;&lt;br /&gt;
$profile-&amp;gt;profile_value=&#039;Inserting a record using insertObject()&#039;;&lt;br /&gt;
$profile-&amp;gt;ordering=1;&lt;br /&gt;
&lt;br /&gt;
try {&lt;br /&gt;
    // Insert the object into the user profile table.&lt;br /&gt;
    $result = JFactory::getDbo()-&amp;gt;insertObject(&#039;#__user_profiles&#039;, $profile);&lt;br /&gt;
catch (Exception $e) {&lt;br /&gt;
    // catch any errors.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice here that we do not need to escape the table name; the insertObject method does this for us.&lt;br /&gt;
&lt;br /&gt;
The insertObject method will throw a [http://php.net/manual/en/class.runtimeexception.php RuntimeException] if there is a problem inserting the record into the database table.&lt;br /&gt;
&lt;br /&gt;
====Updating a Record====&lt;br /&gt;
&lt;br /&gt;
====Deleting a Record====&lt;/div&gt;</summary>
		<author><name>Haydenyoung</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J2.5:Accessing_the_database_using_JDatabase&amp;diff=77641</id>
		<title>J2.5:Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J2.5:Accessing_the_database_using_JDatabase&amp;diff=77641"/>
		<updated>2012-11-20T08:32:45Z</updated>

		<summary type="html">&lt;p&gt;Haydenyoung: /* Inserting a Record */ Inserting a record using insertObject.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{inuse}}&lt;br /&gt;
&lt;br /&gt;
Joomla provides a sophisticated database abstraction layer to simplify the usage for third party developers. New versions of the Joomla Platform API provide additional functionality which extends the database layer further, and includes features such as connectors to a greater variety of database servers and the query chaining to improve readability of connection code and simplify SQL coding.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
&lt;br /&gt;
Joomla can use different kinds of SQL database systems and run in a variety of environments with different table-prefixes. In addition to these functions, the class automatically creates the database connection. Besides instantiating the object you need just two lines of code to get a result from the database in a variety of formats. Using the Joomla database layer ensures a maximum of compatibility and flexibility for your extension.&lt;br /&gt;
&lt;br /&gt;
===The Connection===&lt;br /&gt;
&lt;br /&gt;
Joomla supports a generic interface for connecting to the database, hiding the intricacies of a specific database server from the Framework developer, thus making Joomla code easier to port from one platform to another.&lt;br /&gt;
&lt;br /&gt;
To obtain a database connection, use the [http://api.joomla.org/Joomla-Platform/JFactory.html#getDbo JFactory getDbo] static method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The getDbo method will return a reference to a high-level abstract class called [http://api.joomla.org/Joomla-Platform/Database/JDatabaseDriver.html JDatabaseDriver]; the Joomla Framework hides the specifics of the database server, which is loaded dynamically by getDbo via Joomla&#039;s configuration file.&lt;br /&gt;
&lt;br /&gt;
Alternatively, if your class inherits from the JModel (or a decendent class) getDbo method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = $this-&amp;gt;getDbo();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{JVer|3.0}} users should note that the JModel class has been renamed JModelLegacy and will eventually be deprecated in favour of the class JModelBase. Therefore, any Joomla 3.0 core or extension code should use JFactory::getDbo() to obtain a database connection.&lt;br /&gt;
&lt;br /&gt;
===The Query===&lt;br /&gt;
&lt;br /&gt;
Joomla&#039;s database querying has changed since the new Joomla Framework was introduced  &amp;quot;query chaining&amp;quot; is now the recommended method for building database queries (although string queries are still supported).&lt;br /&gt;
&lt;br /&gt;
Query chaining refers to a method of connecting a number of methods, one after the other, with each method returning an object that can support the next method, improving readability and simplifying code.&lt;br /&gt;
&lt;br /&gt;
To obtain a new instance of the JDatabaseQuery class we use the JDatabaseDriver getQuery method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The JDatabaseDriver::getQuery takes an optional argument, $new, which can be true or false (the default being false).&lt;br /&gt;
&lt;br /&gt;
To query our data source we can call a number of JDatabaseQuery methods; these methods encapsulate the data source&#039;s query language (in most cases SQL), hiding query-specific syntax from the developer and increasing the portability of the developer&#039;s source code. &lt;br /&gt;
&lt;br /&gt;
Some of the more frequently used methods include; select, from, join, where and order. There are also methods such as insert, update and delete for modifying records in the data store. By chaining these and other method calls, you can create almost any query against your data store without compromising portability of your code.&lt;br /&gt;
&lt;br /&gt;
====Selecting Records from a Single Table====&lt;br /&gt;
&lt;br /&gt;
Below is an example of creating a database query using the JDatabaseQuery class. Using the select, from, where and order methods, we can create queries which are flexible, easily readable and portable:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Select all records from the user profile table where key begins with &amp;quot;custom.&amp;quot;.&lt;br /&gt;
// Order it by the ordering field.&lt;br /&gt;
$query-&amp;gt;select(array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;));&lt;br /&gt;
$query-&amp;gt;from(&#039;#__user_profiles&#039;);&lt;br /&gt;
$query-&amp;gt;where(&#039;profile_key LIKE \&#039;custom.%\&#039;&#039;);&lt;br /&gt;
$query-&amp;gt;order(&#039;ordering ASC&#039;);&lt;br /&gt;
&lt;br /&gt;
// Reset the query using our newly populated query object.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
// Load the results as a list of stdClass objects.&lt;br /&gt;
$results = $db-&amp;gt;loadObjectList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The query can also be chained to simplify further:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;select(array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;))&lt;br /&gt;
    -&amp;gt;from(&#039;#__user_profiles&#039;)&lt;br /&gt;
    -&amp;gt;where(&#039;profile_key LIKE \&#039;custom.%\&#039;&#039;)&lt;br /&gt;
    -&amp;gt;order(&#039;ordering ASC&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Chaining can become useful when queries become longer and more complex.&lt;br /&gt;
&lt;br /&gt;
====Selecting Records from Multiple Tables====&lt;br /&gt;
&lt;br /&gt;
Using the JDatabaseQuery&#039;s [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#join join] methods, we can select records from multiple related tables. The generic &amp;quot;join&amp;quot; method takes two arguments; the join &amp;quot;type&amp;quot; (inner, outer, left, right) and the join condition. In the following example you will notice that we can use all of the keywords we would normally use if we were writing a native SQL query, including the AS keyword for aliasing tables and the ON keyword for creating relationships between tables. Also note that the table alias is used in all methods which reference table columns (I.e. select, where, order).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Select all articles for users who have a username which starts with &#039;a&#039;.&lt;br /&gt;
// Order it by the created date.&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;select(array(&#039;a.*&#039;, &#039;b.username&#039;, &#039;b.name&#039;))&lt;br /&gt;
    -&amp;gt;from(&#039;#__content AS a&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;INNER&#039;, &#039;#__users AS b ON (a.created_by = b.id)&#039;)&lt;br /&gt;
    -&amp;gt;where(&#039;b.username LIKE \&#039;a%\&#039;&#039;)&lt;br /&gt;
    -&amp;gt;order(&#039;a.created DESC&#039;);&lt;br /&gt;
&lt;br /&gt;
// Reset the query using our newly populated query object.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
// Load the results as a list of stdClass objects.&lt;br /&gt;
$results = $db-&amp;gt;loadObjectList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The join method above enables us to query both the content and user tables, retrieving articles with their author details. There are also convenience methods for [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#innerJoin inner], [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#leftJoin left], [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#rightJoin right] and [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#outerJoin outer] joins.&lt;br /&gt;
&lt;br /&gt;
We can use multiple joins to query across more than two tables:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;select(array(&#039;a.*&#039;, &#039;b.username&#039;, &#039;b.name&#039;, &#039;c.*&#039;, &#039;d.*&#039;))&lt;br /&gt;
    -&amp;gt;from(&#039;#__content AS a&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;INNER&#039;, &#039;#__users AS b ON (a.created_by = b.id)&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;LEFT&#039;, &#039;#__user_profiles AS c ON (b.id = c.user_id)&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;RIGHT&#039;, &#039;#__categories AS d ON (a.catid = d.id)&#039;)&lt;br /&gt;
    -&amp;gt;where(&#039;b.username LIKE \&#039;a%\&#039;&#039;)&lt;br /&gt;
    -&amp;gt;order(&#039;a.created DESC&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice how chaining makes the source code much more readable for these longer queries.&lt;br /&gt;
&lt;br /&gt;
====Inserting a Record====&lt;br /&gt;
&lt;br /&gt;
=====Using SQL=====&lt;br /&gt;
&lt;br /&gt;
The JDatabaseQuery class provides a number of methods for building insert queries, the most common being [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#insert insert], [http://api.joomla.org/11.4/Joomla-Platform/Database/JDatabaseQuery.html#columns columns] and [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#values values].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Insert columns.&lt;br /&gt;
$columns = array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;);&lt;br /&gt;
&lt;br /&gt;
// Insert values.&lt;br /&gt;
$values = array(1001, $db-&amp;gt;quote(&#039;custom.message&#039;), $db-&amp;gt;quote(&#039;Inserting a record using insert()&#039;), 1);&lt;br /&gt;
&lt;br /&gt;
// Prepare the insert query.&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;insert($db-&amp;gt;quoteName(&#039;#__user_profiles&#039;))&lt;br /&gt;
    -&amp;gt;columns($db-&amp;gt;quoteName($columns))&lt;br /&gt;
    -&amp;gt;values(implode(&#039;,&#039;, $values));&lt;br /&gt;
&lt;br /&gt;
// Reset the query using our newly populated query object.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since Joomla 3.0 there has been a change in how the Joomla database class executes a query.&lt;br /&gt;
&lt;br /&gt;
When executing a query in the Joomla Framework 11.4 we use the JDatabase&#039;s [http://api.joomla.org/11.4/Joomla-Platform/Database/JDatabase.html#query query] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
try {&lt;br /&gt;
    // Execute the query in Joomla 2.5.&lt;br /&gt;
    $result = $db-&amp;gt;query();&lt;br /&gt;
} catch (Exception $e) {&lt;br /&gt;
    // catch any database errors.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For Joomla Framework 12.1 and higher, we use the JDatabaseDriver&#039;s [http://api.joomla.org/Joomla-Platform/Database/JDatabaseDriver.html#execute execute] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
try {&lt;br /&gt;
    // Execute the query in Joomla 3.0.&lt;br /&gt;
    $result = $db-&amp;gt;execute();&lt;br /&gt;
} catch (Exception $e) {&lt;br /&gt;
    // catch any database errors.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since the phase out of PHP 4.x support, PHP&#039;s exception model has been widely implemented in the Joomla Framework. Therefore, we can wrap our database execution in a try...catch block to catch any unexpected database errors that may arise. Any database-specific errors will be thrown using an instance of the [http://api.joomla.org/Joomla-Platform/Database/JDatabaseException.html JDatabaseException] class although it is best practice to catch the generic Exception object from which JDatabaseException inherits.&lt;br /&gt;
&lt;br /&gt;
=====Using an Object=====&lt;br /&gt;
&lt;br /&gt;
The JDatabaseDriver also class provides us with a convenience method for saving an object directly to the database allowing us to add a record to a table without writing a single line of SQL.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$profile = new stdClass();&lt;br /&gt;
$profile-&amp;gt;user_id = 42;&lt;br /&gt;
$profile-&amp;gt;profile_key=&#039;custom.message&#039;;&lt;br /&gt;
$profile-&amp;gt;profile_value=&#039;Inserting a record using insertObject()&#039;;&lt;br /&gt;
$profile-&amp;gt;ordering=1;&lt;br /&gt;
&lt;br /&gt;
try {&lt;br /&gt;
    $result = $db-&amp;gt;insertObject(&#039;#__user_profiles&#039;, $profile);&lt;br /&gt;
catch (Exception $e) {&lt;br /&gt;
    // catch any errors.&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The insertObject method will throw a PHP [http://php.net/manual/en/class.runtimeexception.php RuntimeException] if there is a problem inserting the record into the database table.&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice here that we do not need to escape the table name; the insertObject method does this for us.&lt;br /&gt;
&lt;br /&gt;
====Updating a Record====&lt;br /&gt;
&lt;br /&gt;
====Deleting a Record====&lt;/div&gt;</summary>
		<author><name>Haydenyoung</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J3.x:Accessing_the_database_using_JDatabase&amp;diff=77640</id>
		<title>J3.x:Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J3.x:Accessing_the_database_using_JDatabase&amp;diff=77640"/>
		<updated>2012-11-20T07:26:00Z</updated>

		<summary type="html">&lt;p&gt;Haydenyoung: 3.0 documentation.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{underconstruction}}&lt;br /&gt;
&lt;br /&gt;
Joomla provides a sophisticated database abstraction layer to simplify the usage for third party developers. New versions of the Joomla Platform API provide additional functionality which extends the database layer further, and includes features such as connectors to a greater variety of database servers and the query chaining to improve readability of connection code and simplify SQL coding.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
&lt;br /&gt;
Joomla can use different kinds of SQL database systems and run in a variety of environments with different table-prefixes. In addition to these functions, the class automatically creates the database connection. Besides instantiating the object you need just two lines of code to get a result from the database in a variety of formats. Using the Joomla database layer ensures a maximum of compatibility and flexibility for your extension.&lt;br /&gt;
&lt;br /&gt;
===The Connection===&lt;br /&gt;
&lt;br /&gt;
Joomla supports a generic interface for connecting to the database, hiding the intricacies of a specific database server from the Framework developer, thus making Joomla code easier to port from one platform to another.&lt;br /&gt;
&lt;br /&gt;
To obtain a database connection, use the [http://api.joomla.org/Joomla-Platform/JFactory.html#getDbo JFactory getDbo] static method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The getDbo method will return a reference to a high-level abstract class called [http://api.joomla.org/Joomla-Platform/Database/JDatabaseDriver.html JDatabaseDriver]; the Joomla Framework hides the specifics of the database server, which is loaded dynamically by getDbo via Joomla&#039;s configuration file.&lt;br /&gt;
&lt;br /&gt;
Alternatively, if your class inherits from the JModel (or a decendent class) getDbo method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = $this-&amp;gt;getDbo();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{JVer|3.0}} users should note that the JModel class has been renamed JModelLegacy and will eventually be deprecated in favour of the class JModelBase. Therefore, any Joomla 3.0 core or extension code should use JFactory::getDbo() to obtain a database connection.&lt;br /&gt;
&lt;br /&gt;
===The Query===&lt;br /&gt;
&lt;br /&gt;
Joomla&#039;s database querying has changed since the new Joomla Framework was introduced  &amp;quot;query chaining&amp;quot; is now the recommended method for building database queries (although string queries are still supported).&lt;br /&gt;
&lt;br /&gt;
Query chaining refers to a method of connecting a number of methods, one after the other, with each method returning an object that can support the next method, improving readability and simplifying code.&lt;br /&gt;
&lt;br /&gt;
To obtain a new instance of the JDatabaseQuery class we use the JDatabaseDriver getQuery method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The JDatabaseDriver::getQuery takes an optional argument, $new, which can be true or false (the default being false).&lt;br /&gt;
&lt;br /&gt;
To query our data source we can call a number of JDatabaseQuery methods; these methods encapsulate the data source&#039;s query language (in most cases SQL), hiding query-specific syntax from the developer and increasing the portability of the developer&#039;s source code. &lt;br /&gt;
&lt;br /&gt;
Some of the more frequently used methods include; select, from, join, where and order. There are also methods such as insert, update and delete for modifying records in the data store. By chaining these and other method calls, you can create almost any query against your data store without compromising portability of your code.&lt;br /&gt;
&lt;br /&gt;
====Selecting Records from a Single Table====&lt;br /&gt;
&lt;br /&gt;
Below is an example of creating a database query using the JDatabaseQuery class. Using the select, from, where and order methods, we can create queries which are flexible, easily readable and portable:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Select all records from the user profile table where key begins with &amp;quot;custom.&amp;quot;.&lt;br /&gt;
// Order it by the ordering field.&lt;br /&gt;
$query-&amp;gt;select(array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;));&lt;br /&gt;
$query-&amp;gt;from(&#039;#__user_profiles&#039;);&lt;br /&gt;
$query-&amp;gt;where(&#039;profile_key LIKE \&#039;custom.%\&#039;&#039;);&lt;br /&gt;
$query-&amp;gt;order(&#039;ordering ASC&#039;);&lt;br /&gt;
&lt;br /&gt;
// Reset the query using our newly populated query object.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
// Load the results as a list of stdClass objects.&lt;br /&gt;
$results = $db-&amp;gt;loadObjectList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The query can also be chained to simplify further:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;select(array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;))&lt;br /&gt;
    -&amp;gt;from(&#039;#__user_profiles&#039;)&lt;br /&gt;
    -&amp;gt;where(&#039;profile_key LIKE \&#039;custom.%\&#039;&#039;)&lt;br /&gt;
    -&amp;gt;order(&#039;ordering ASC&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Chaining can become useful when queries become longer and more complex.&lt;br /&gt;
&lt;br /&gt;
====Selecting Records from Multiple Tables====&lt;br /&gt;
&lt;br /&gt;
Using the JDatabaseQuery&#039;s [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#join join] methods, we can select records from multiple related tables. The generic &amp;quot;join&amp;quot; method takes two arguments; the join &amp;quot;type&amp;quot; (inner, outer, left, right) and the join condition. In the following example you will notice that we can use all of the keywords we would normally use if we were writing a native SQL query, including the AS keyword for aliasing tables and the ON keyword for creating relationships between tables. Also note that the table alias is used in all methods which reference table columns (I.e. select, where, order).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Select all articles for users who have a username which starts with &#039;a&#039;.&lt;br /&gt;
// Order it by the created date.&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;select(array(&#039;a.*&#039;, &#039;b.username&#039;, &#039;b.name&#039;))&lt;br /&gt;
    -&amp;gt;from(&#039;#__content AS a&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;INNER&#039;, &#039;#__users AS b ON (a.created_by = b.id)&#039;)&lt;br /&gt;
    -&amp;gt;where(&#039;b.username LIKE \&#039;a%\&#039;&#039;)&lt;br /&gt;
    -&amp;gt;order(&#039;a.created DESC&#039;);&lt;br /&gt;
&lt;br /&gt;
// Reset the query using our newly populated query object.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
// Load the results as a list of stdClass objects.&lt;br /&gt;
$results = $db-&amp;gt;loadObjectList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The join method above enables us to query both the content and user tables, retrieving articles with their author details. There are also convenience methods for [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#innerJoin inner], [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#leftJoin left], [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#rightJoin right] and [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#outerJoin outer] joins.&lt;br /&gt;
&lt;br /&gt;
We can use multiple joins to query across more than two tables:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;select(array(&#039;a.*&#039;, &#039;b.username&#039;, &#039;b.name&#039;, &#039;c.*&#039;, &#039;d.*&#039;))&lt;br /&gt;
    -&amp;gt;from(&#039;#__content AS a&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;INNER&#039;, &#039;#__users AS b ON (a.created_by = b.id)&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;LEFT&#039;, &#039;#__user_profiles AS c ON (b.id = c.user_id)&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;RIGHT&#039;, &#039;#__categories AS d ON (a.catid = d.id)&#039;)&lt;br /&gt;
    -&amp;gt;where(&#039;b.username LIKE \&#039;a%\&#039;&#039;)&lt;br /&gt;
    -&amp;gt;order(&#039;a.created DESC&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice how chaining makes the source code much more readable for these longer queries.&lt;br /&gt;
&lt;br /&gt;
====Inserting a Record====&lt;br /&gt;
&lt;br /&gt;
The JDatabaseQuery class provides a number of methods for building insert queries, the most common being [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#insert insert], [http://api.joomla.org/11.4/Joomla-Platform/Database/JDatabaseQuery.html#columns columns] and [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#values values].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Insert columns.&lt;br /&gt;
$columns = array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;);&lt;br /&gt;
&lt;br /&gt;
// Insert values.&lt;br /&gt;
$values = array(1001, $db-&amp;gt;quote(&#039;custom.message&#039;), $db-&amp;gt;quote(&#039;Inserting a record using insert()&#039;), 1);&lt;br /&gt;
&lt;br /&gt;
// Prepare the insert query.&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;insert($db-&amp;gt;quoteName(&#039;#__user_profiles&#039;))&lt;br /&gt;
    -&amp;gt;columns($db-&amp;gt;quoteName($columns))&lt;br /&gt;
    -&amp;gt;values(implode(&#039;,&#039;, $values));&lt;br /&gt;
&lt;br /&gt;
// Reset the query using our newly populated query object.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since Joomla 3.0 there has been a change in how the Joomla database class executes a query.&lt;br /&gt;
&lt;br /&gt;
When executing a query in the Joomla Framework 11.4 we use the JDatabase&#039;s [http://api.joomla.org/11.4/Joomla-Platform/Database/JDatabase.html#query query] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
try {&lt;br /&gt;
    // Execute the query in Joomla 2.5.&lt;br /&gt;
    $result = $db-&amp;gt;query();&lt;br /&gt;
} catch (Exception $e) {&lt;br /&gt;
    // catch any database errors.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For Joomla Framework 12.1 and higher, we use the JDatabaseDriver&#039;s [http://api.joomla.org/Joomla-Platform/Database/JDatabaseDriver.html#execute execute] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
try {&lt;br /&gt;
    // Execute the query in Joomla 3.0.&lt;br /&gt;
    $result = $db-&amp;gt;execute();&lt;br /&gt;
} catch (Exception $e) {&lt;br /&gt;
    // catch any database errors.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since the phase out of PHP 4.x support, PHP&#039;s exception model has been widely implemented in the Joomla Framework. Therefore, we can wrap our database execution in a try...catch block to catch any unexpected database errors that may arise. Any database-specific errors will be thrown using an instance of the [http://api.joomla.org/Joomla-Platform/Database/JDatabaseException.html JDatabaseException] class although it is best practice to catch the generic Exception object from which JDatabaseException inherits.&lt;br /&gt;
&lt;br /&gt;
====Updating a Record====&lt;br /&gt;
&lt;br /&gt;
====Deleting a Record====&lt;/div&gt;</summary>
		<author><name>Haydenyoung</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J2.5:Accessing_the_database_using_JDatabase&amp;diff=77639</id>
		<title>J2.5:Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J2.5:Accessing_the_database_using_JDatabase&amp;diff=77639"/>
		<updated>2012-11-20T06:33:03Z</updated>

		<summary type="html">&lt;p&gt;Haydenyoung: /* Inserting a Record */ Inserting a record using a chained query.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{inuse}}&lt;br /&gt;
&lt;br /&gt;
Joomla provides a sophisticated database abstraction layer to simplify the usage for third party developers. New versions of the Joomla Platform API provide additional functionality which extends the database layer further, and includes features such as connectors to a greater variety of database servers and the query chaining to improve readability of connection code and simplify SQL coding.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
&lt;br /&gt;
Joomla can use different kinds of SQL database systems and run in a variety of environments with different table-prefixes. In addition to these functions, the class automatically creates the database connection. Besides instantiating the object you need just two lines of code to get a result from the database in a variety of formats. Using the Joomla database layer ensures a maximum of compatibility and flexibility for your extension.&lt;br /&gt;
&lt;br /&gt;
===The Connection===&lt;br /&gt;
&lt;br /&gt;
Joomla supports a generic interface for connecting to the database, hiding the intricacies of a specific database server from the Framework developer, thus making Joomla code easier to port from one platform to another.&lt;br /&gt;
&lt;br /&gt;
To obtain a database connection, use the [http://api.joomla.org/Joomla-Platform/JFactory.html#getDbo JFactory getDbo] static method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The getDbo method will return a reference to a high-level abstract class called [http://api.joomla.org/Joomla-Platform/Database/JDatabaseDriver.html JDatabaseDriver]; the Joomla Framework hides the specifics of the database server, which is loaded dynamically by getDbo via Joomla&#039;s configuration file.&lt;br /&gt;
&lt;br /&gt;
Alternatively, if your class inherits from the JModel (or a decendent class) getDbo method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = $this-&amp;gt;getDbo();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{JVer|3.0}} users should note that the JModel class has been renamed JModelLegacy and will eventually be deprecated in favour of the class JModelBase. Therefore, any Joomla 3.0 core or extension code should use JFactory::getDbo() to obtain a database connection.&lt;br /&gt;
&lt;br /&gt;
===The Query===&lt;br /&gt;
&lt;br /&gt;
Joomla&#039;s database querying has changed since the new Joomla Framework was introduced  &amp;quot;query chaining&amp;quot; is now the recommended method for building database queries (although string queries are still supported).&lt;br /&gt;
&lt;br /&gt;
Query chaining refers to a method of connecting a number of methods, one after the other, with each method returning an object that can support the next method, improving readability and simplifying code.&lt;br /&gt;
&lt;br /&gt;
To obtain a new instance of the JDatabaseQuery class we use the JDatabaseDriver getQuery method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The JDatabaseDriver::getQuery takes an optional argument, $new, which can be true or false (the default being false).&lt;br /&gt;
&lt;br /&gt;
To query our data source we can call a number of JDatabaseQuery methods; these methods encapsulate the data source&#039;s query language (in most cases SQL), hiding query-specific syntax from the developer and increasing the portability of the developer&#039;s source code. &lt;br /&gt;
&lt;br /&gt;
Some of the more frequently used methods include; select, from, join, where and order. There are also methods such as insert, update and delete for modifying records in the data store. By chaining these and other method calls, you can create almost any query against your data store without compromising portability of your code.&lt;br /&gt;
&lt;br /&gt;
====Selecting Records from a Single Table====&lt;br /&gt;
&lt;br /&gt;
Below is an example of creating a database query using the JDatabaseQuery class. Using the select, from, where and order methods, we can create queries which are flexible, easily readable and portable:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Select all records from the user profile table where key begins with &amp;quot;custom.&amp;quot;.&lt;br /&gt;
// Order it by the ordering field.&lt;br /&gt;
$query-&amp;gt;select(array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;));&lt;br /&gt;
$query-&amp;gt;from(&#039;#__user_profiles&#039;);&lt;br /&gt;
$query-&amp;gt;where(&#039;profile_key LIKE \&#039;custom.%\&#039;&#039;);&lt;br /&gt;
$query-&amp;gt;order(&#039;ordering ASC&#039;);&lt;br /&gt;
&lt;br /&gt;
// Reset the query using our newly populated query object.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
// Load the results as a list of stdClass objects.&lt;br /&gt;
$results = $db-&amp;gt;loadObjectList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The query can also be chained to simplify further:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;select(array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;))&lt;br /&gt;
    -&amp;gt;from(&#039;#__user_profiles&#039;)&lt;br /&gt;
    -&amp;gt;where(&#039;profile_key LIKE \&#039;custom.%\&#039;&#039;)&lt;br /&gt;
    -&amp;gt;order(&#039;ordering ASC&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Chaining can become useful when queries become longer and more complex.&lt;br /&gt;
&lt;br /&gt;
====Selecting Records from Multiple Tables====&lt;br /&gt;
&lt;br /&gt;
Using the JDatabaseQuery&#039;s [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#join join] methods, we can select records from multiple related tables. The generic &amp;quot;join&amp;quot; method takes two arguments; the join &amp;quot;type&amp;quot; (inner, outer, left, right) and the join condition. In the following example you will notice that we can use all of the keywords we would normally use if we were writing a native SQL query, including the AS keyword for aliasing tables and the ON keyword for creating relationships between tables. Also note that the table alias is used in all methods which reference table columns (I.e. select, where, order).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Select all articles for users who have a username which starts with &#039;a&#039;.&lt;br /&gt;
// Order it by the created date.&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;select(array(&#039;a.*&#039;, &#039;b.username&#039;, &#039;b.name&#039;))&lt;br /&gt;
    -&amp;gt;from(&#039;#__content AS a&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;INNER&#039;, &#039;#__users AS b ON (a.created_by = b.id)&#039;)&lt;br /&gt;
    -&amp;gt;where(&#039;b.username LIKE \&#039;a%\&#039;&#039;)&lt;br /&gt;
    -&amp;gt;order(&#039;a.created DESC&#039;);&lt;br /&gt;
&lt;br /&gt;
// Reset the query using our newly populated query object.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
// Load the results as a list of stdClass objects.&lt;br /&gt;
$results = $db-&amp;gt;loadObjectList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The join method above enables us to query both the content and user tables, retrieving articles with their author details. There are also convenience methods for [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#innerJoin inner], [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#leftJoin left], [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#rightJoin right] and [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#outerJoin outer] joins.&lt;br /&gt;
&lt;br /&gt;
We can use multiple joins to query across more than two tables:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;select(array(&#039;a.*&#039;, &#039;b.username&#039;, &#039;b.name&#039;, &#039;c.*&#039;, &#039;d.*&#039;))&lt;br /&gt;
    -&amp;gt;from(&#039;#__content AS a&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;INNER&#039;, &#039;#__users AS b ON (a.created_by = b.id)&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;LEFT&#039;, &#039;#__user_profiles AS c ON (b.id = c.user_id)&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;RIGHT&#039;, &#039;#__categories AS d ON (a.catid = d.id)&#039;)&lt;br /&gt;
    -&amp;gt;where(&#039;b.username LIKE \&#039;a%\&#039;&#039;)&lt;br /&gt;
    -&amp;gt;order(&#039;a.created DESC&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice how chaining makes the source code much more readable for these longer queries.&lt;br /&gt;
&lt;br /&gt;
====Inserting a Record====&lt;br /&gt;
&lt;br /&gt;
The JDatabaseQuery class provides a number of methods for building insert queries, the most common being [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#insert insert], [http://api.joomla.org/11.4/Joomla-Platform/Database/JDatabaseQuery.html#columns columns] and [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#values values].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Insert columns.&lt;br /&gt;
$columns = array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;);&lt;br /&gt;
&lt;br /&gt;
// Insert values.&lt;br /&gt;
$values = array(1001, $db-&amp;gt;quote(&#039;custom.message&#039;), $db-&amp;gt;quote(&#039;Inserting a record using insert()&#039;), 1);&lt;br /&gt;
&lt;br /&gt;
// Prepare the insert query.&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;insert($db-&amp;gt;quoteName(&#039;#__user_profiles&#039;))&lt;br /&gt;
    -&amp;gt;columns($db-&amp;gt;quoteName($columns))&lt;br /&gt;
    -&amp;gt;values(implode(&#039;,&#039;, $values));&lt;br /&gt;
&lt;br /&gt;
// Reset the query using our newly populated query object.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since Joomla 3.0 there has been a change in how the Joomla database class executes a query.&lt;br /&gt;
&lt;br /&gt;
When executing a query in the Joomla Framework 11.4 we use the JDatabase&#039;s [http://api.joomla.org/11.4/Joomla-Platform/Database/JDatabase.html#query query] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
try {&lt;br /&gt;
    // Execute the query in Joomla 2.5.&lt;br /&gt;
    $result = $db-&amp;gt;query();&lt;br /&gt;
} catch (Exception $e) {&lt;br /&gt;
    // catch any database errors.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For Joomla Framework 12.1 and higher, we use the JDatabaseDriver&#039;s [http://api.joomla.org/Joomla-Platform/Database/JDatabaseDriver.html#execute execute] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
try {&lt;br /&gt;
    // Execute the query in Joomla 3.0.&lt;br /&gt;
    $result = $db-&amp;gt;execute();&lt;br /&gt;
} catch (Exception $e) {&lt;br /&gt;
    // catch any database errors.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since the phase out of PHP 4.x support, PHP&#039;s exception model has been widely implemented in the Joomla Framework. Therefore, we can wrap our database execution in a try...catch block to catch any unexpected database errors that may arise. Any database-specific errors will be thrown using an instance of the [http://api.joomla.org/Joomla-Platform/Database/JDatabaseException.html JDatabaseException] class although it is best practice to catch the generic Exception object from which JDatabaseException inherits.&lt;br /&gt;
&lt;br /&gt;
====Updating a Record====&lt;br /&gt;
&lt;br /&gt;
====Deleting a Record====&lt;/div&gt;</summary>
		<author><name>Haydenyoung</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J2.5:Accessing_the_database_using_JDatabase&amp;diff=77638</id>
		<title>J2.5:Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J2.5:Accessing_the_database_using_JDatabase&amp;diff=77638"/>
		<updated>2012-11-20T04:27:42Z</updated>

		<summary type="html">&lt;p&gt;Haydenyoung: /* The Connection */ Links to various useful classes and methods.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{inuse}}&lt;br /&gt;
&lt;br /&gt;
Joomla provides a sophisticated database abstraction layer to simplify the usage for third party developers. New versions of the Joomla Platform API provide additional functionality which extends the database layer further, and includes features such as connectors to a greater variety of database servers and the query chaining to improve readability of connection code and simplify SQL coding.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
&lt;br /&gt;
Joomla can use different kinds of SQL database systems and run in a variety of environments with different table-prefixes. In addition to these functions, the class automatically creates the database connection. Besides instantiating the object you need just two lines of code to get a result from the database in a variety of formats. Using the Joomla database layer ensures a maximum of compatibility and flexibility for your extension.&lt;br /&gt;
&lt;br /&gt;
===The Connection===&lt;br /&gt;
&lt;br /&gt;
Joomla supports a generic interface for connecting to the database, hiding the intricacies of a specific database server from the Framework developer, thus making Joomla code easier to port from one platform to another.&lt;br /&gt;
&lt;br /&gt;
To obtain a database connection, use the [http://api.joomla.org/Joomla-Platform/JFactory.html#getDbo JFactory getDbo] static method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The getDbo method will return a reference to a high-level abstract class called [http://api.joomla.org/Joomla-Platform/Database/JDatabaseDriver.html JDatabaseDriver]; the Joomla Framework hides the specifics of the database server, which is loaded dynamically by getDbo via Joomla&#039;s configuration file.&lt;br /&gt;
&lt;br /&gt;
Alternatively, if your class inherits from the JModel (or a decendent class) getDbo method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = $this-&amp;gt;getDbo();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{JVer|3.0}} users should note that the JModel class has been renamed JModelLegacy and will eventually be deprecated in favour of the class JModelBase. Therefore, any Joomla 3.0 core or extension code should use JFactory::getDbo() to obtain a database connection.&lt;br /&gt;
&lt;br /&gt;
===The Query===&lt;br /&gt;
&lt;br /&gt;
Joomla&#039;s database querying has changed since the new Joomla Framework was introduced  &amp;quot;query chaining&amp;quot; is now the recommended method for building database queries (although string queries are still supported).&lt;br /&gt;
&lt;br /&gt;
Query chaining refers to a method of connecting a number of methods, one after the other, with each method returning an object that can support the next method, improving readability and simplifying code.&lt;br /&gt;
&lt;br /&gt;
To obtain a new instance of the JDatabaseQuery class we use the JDatabaseDriver getQuery method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The JDatabaseDriver::getQuery takes an optional argument, $new, which can be true or false (the default being false).&lt;br /&gt;
&lt;br /&gt;
To query our data source we can call a number of JDatabaseQuery methods; these methods encapsulate the data source&#039;s query language (in most cases SQL), hiding query-specific syntax from the developer and increasing the portability of the developer&#039;s source code. &lt;br /&gt;
&lt;br /&gt;
Some of the more frequently used methods include; select, from, join, where and order. There are also methods such as insert, update and delete for modifying records in the data store. By chaining these and other method calls, you can create almost any query against your data store without compromising portability of your code.&lt;br /&gt;
&lt;br /&gt;
====Selecting Records from a Single Table====&lt;br /&gt;
&lt;br /&gt;
Below is an example of creating a database query using the JDatabaseQuery class. Using the select, from, where and order methods, we can create queries which are flexible, easily readable and portable:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Select all records from the user profile table where key begins with &amp;quot;custom.&amp;quot;.&lt;br /&gt;
// Order it by the ordering field.&lt;br /&gt;
$query-&amp;gt;select(array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;));&lt;br /&gt;
$query-&amp;gt;from(&#039;#__user_profiles&#039;);&lt;br /&gt;
$query-&amp;gt;where(&#039;profile_key LIKE \&#039;custom.%\&#039;&#039;);&lt;br /&gt;
$query-&amp;gt;order(&#039;ordering ASC&#039;);&lt;br /&gt;
&lt;br /&gt;
// Reset the query using our newly populated query object.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
// Load the results as a list of stdClass objects.&lt;br /&gt;
$results = $db-&amp;gt;loadObjectList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The query can also be chained to simplify further:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;select(array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;))&lt;br /&gt;
    -&amp;gt;from(&#039;#__user_profiles&#039;)&lt;br /&gt;
    -&amp;gt;where(&#039;profile_key LIKE \&#039;custom.%\&#039;&#039;)&lt;br /&gt;
    -&amp;gt;order(&#039;ordering ASC&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Chaining can become useful when queries become longer and more complex.&lt;br /&gt;
&lt;br /&gt;
====Selecting Records from Multiple Tables====&lt;br /&gt;
&lt;br /&gt;
Using the JDatabaseQuery&#039;s [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#join join] methods, we can select records from multiple related tables. The generic &amp;quot;join&amp;quot; method takes two arguments; the join &amp;quot;type&amp;quot; (inner, outer, left, right) and the join condition. In the following example you will notice that we can use all of the keywords we would normally use if we were writing a native SQL query, including the AS keyword for aliasing tables and the ON keyword for creating relationships between tables. Also note that the table alias is used in all methods which reference table columns (I.e. select, where, order).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Select all articles for users who have a username which starts with &#039;a&#039;.&lt;br /&gt;
// Order it by the created date.&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;select(array(&#039;a.*&#039;, &#039;b.username&#039;, &#039;b.name&#039;))&lt;br /&gt;
    -&amp;gt;from(&#039;#__content AS a&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;INNER&#039;, &#039;#__users AS b ON (a.created_by = b.id)&#039;)&lt;br /&gt;
    -&amp;gt;where(&#039;b.username LIKE \&#039;a%\&#039;&#039;)&lt;br /&gt;
    -&amp;gt;order(&#039;a.created DESC&#039;);&lt;br /&gt;
&lt;br /&gt;
// Reset the query using our newly populated query object.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
// Load the results as a list of stdClass objects.&lt;br /&gt;
$results = $db-&amp;gt;loadObjectList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The join method above enables us to query both the content and user tables, retrieving articles with their author details. There are also convenience methods for [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#innerJoin inner], [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#leftJoin left], [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#rightJoin right] and [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#outerJoin outer] joins.&lt;br /&gt;
&lt;br /&gt;
We can use multiple joins to query across more than two tables:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;select(array(&#039;a.*&#039;, &#039;b.username&#039;, &#039;b.name&#039;, &#039;c.*&#039;, &#039;d.*&#039;))&lt;br /&gt;
    -&amp;gt;from(&#039;#__content AS a&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;INNER&#039;, &#039;#__users AS b ON (a.created_by = b.id)&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;LEFT&#039;, &#039;#__user_profiles AS c ON (b.id = c.user_id)&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;RIGHT&#039;, &#039;#__categories AS d ON (a.catid = d.id)&#039;)&lt;br /&gt;
    -&amp;gt;where(&#039;b.username LIKE \&#039;a%\&#039;&#039;)&lt;br /&gt;
    -&amp;gt;order(&#039;a.created DESC&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice how chaining makes the source code much more readable for these longer queries.&lt;br /&gt;
&lt;br /&gt;
====Inserting a Record====&lt;br /&gt;
&lt;br /&gt;
====Updating a Record====&lt;br /&gt;
&lt;br /&gt;
====Deleting a Record====&lt;/div&gt;</summary>
		<author><name>Haydenyoung</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J2.5:Accessing_the_database_using_JDatabase&amp;diff=77637</id>
		<title>J2.5:Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J2.5:Accessing_the_database_using_JDatabase&amp;diff=77637"/>
		<updated>2012-11-20T04:13:13Z</updated>

		<summary type="html">&lt;p&gt;Haydenyoung: /* Selecting Records from Multiple Tables */ Chaining a query with multiple joins.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{inuse}}&lt;br /&gt;
&lt;br /&gt;
Joomla provides a sophisticated database abstraction layer to simplify the usage for third party developers. New versions of the Joomla Platform API provide additional functionality which extends the database layer further, and includes features such as connectors to a greater variety of database servers and the query chaining to improve readability of connection code and simplify SQL coding.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
&lt;br /&gt;
Joomla can use different kinds of SQL database systems and run in a variety of environments with different table-prefixes. In addition to these functions, the class automatically creates the database connection. Besides instantiating the object you need just two lines of code to get a result from the database in a variety of formats. Using the Joomla database layer ensures a maximum of compatibility and flexibility for your extension.&lt;br /&gt;
&lt;br /&gt;
===The Connection===&lt;br /&gt;
&lt;br /&gt;
Joomla supports a generic interface for connecting to the database, hiding the intricacies of a specific database server from the Framework developer, thus making Joomla code easier to port from one database to another.&lt;br /&gt;
&lt;br /&gt;
To obtain a database connection, use the JFactory getDbo static method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Alternatively, if your class inherits from the JModel (or a decendent class) getDbo method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = $this-&amp;gt;getDbo();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{JVer|3.0}} users should note that the JModel class has been renamed JModelLegacy and will eventually be deprecated in favour of the class JModelBase. Therefore, any Joomla 3.0 core or extension code should use JFactory::getDbo() to obtain a database connection.&lt;br /&gt;
&lt;br /&gt;
===The Query===&lt;br /&gt;
&lt;br /&gt;
Joomla&#039;s database querying has changed since the new Joomla Framework was introduced  &amp;quot;query chaining&amp;quot; is now the recommended method for building database queries (although string queries are still supported).&lt;br /&gt;
&lt;br /&gt;
Query chaining refers to a method of connecting a number of methods, one after the other, with each method returning an object that can support the next method, improving readability and simplifying code.&lt;br /&gt;
&lt;br /&gt;
To obtain a new instance of the JDatabaseQuery class we use the JDatabaseDriver getQuery method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The JDatabaseDriver::getQuery takes an optional argument, $new, which can be true or false (the default being false).&lt;br /&gt;
&lt;br /&gt;
To query our data source we can call a number of JDatabaseQuery methods; these methods encapsulate the data source&#039;s query language (in most cases SQL), hiding query-specific syntax from the developer and increasing the portability of the developer&#039;s source code. &lt;br /&gt;
&lt;br /&gt;
Some of the more frequently used methods include; select, from, join, where and order. There are also methods such as insert, update and delete for modifying records in the data store. By chaining these and other method calls, you can create almost any query against your data store without compromising portability of your code.&lt;br /&gt;
&lt;br /&gt;
====Selecting Records from a Single Table====&lt;br /&gt;
&lt;br /&gt;
Below is an example of creating a database query using the JDatabaseQuery class. Using the select, from, where and order methods, we can create queries which are flexible, easily readable and portable:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Select all records from the user profile table where key begins with &amp;quot;custom.&amp;quot;.&lt;br /&gt;
// Order it by the ordering field.&lt;br /&gt;
$query-&amp;gt;select(array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;));&lt;br /&gt;
$query-&amp;gt;from(&#039;#__user_profiles&#039;);&lt;br /&gt;
$query-&amp;gt;where(&#039;profile_key LIKE \&#039;custom.%\&#039;&#039;);&lt;br /&gt;
$query-&amp;gt;order(&#039;ordering ASC&#039;);&lt;br /&gt;
&lt;br /&gt;
// Reset the query using our newly populated query object.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
// Load the results as a list of stdClass objects.&lt;br /&gt;
$results = $db-&amp;gt;loadObjectList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The query can also be chained to simplify further:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;select(array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;))&lt;br /&gt;
    -&amp;gt;from(&#039;#__user_profiles&#039;)&lt;br /&gt;
    -&amp;gt;where(&#039;profile_key LIKE \&#039;custom.%\&#039;&#039;)&lt;br /&gt;
    -&amp;gt;order(&#039;ordering ASC&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Chaining can become useful when queries become longer and more complex.&lt;br /&gt;
&lt;br /&gt;
====Selecting Records from Multiple Tables====&lt;br /&gt;
&lt;br /&gt;
Using the JDatabaseQuery&#039;s [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#join join] methods, we can select records from multiple related tables. The generic &amp;quot;join&amp;quot; method takes two arguments; the join &amp;quot;type&amp;quot; (inner, outer, left, right) and the join condition. In the following example you will notice that we can use all of the keywords we would normally use if we were writing a native SQL query, including the AS keyword for aliasing tables and the ON keyword for creating relationships between tables. Also note that the table alias is used in all methods which reference table columns (I.e. select, where, order).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Select all articles for users who have a username which starts with &#039;a&#039;.&lt;br /&gt;
// Order it by the created date.&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;select(array(&#039;a.*&#039;, &#039;b.username&#039;, &#039;b.name&#039;))&lt;br /&gt;
    -&amp;gt;from(&#039;#__content AS a&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;INNER&#039;, &#039;#__users AS b ON (a.created_by = b.id)&#039;)&lt;br /&gt;
    -&amp;gt;where(&#039;b.username LIKE \&#039;a%\&#039;&#039;)&lt;br /&gt;
    -&amp;gt;order(&#039;a.created DESC&#039;);&lt;br /&gt;
&lt;br /&gt;
// Reset the query using our newly populated query object.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
// Load the results as a list of stdClass objects.&lt;br /&gt;
$results = $db-&amp;gt;loadObjectList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The join method above enables us to query both the content and user tables, retrieving articles with their author details. There are also convenience methods for [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#innerJoin inner], [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#leftJoin left], [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#rightJoin right] and [http://api.joomla.org/Joomla-Platform/Database/JDatabaseQuery.html#outerJoin outer] joins.&lt;br /&gt;
&lt;br /&gt;
We can use multiple joins to query across more than two tables:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;select(array(&#039;a.*&#039;, &#039;b.username&#039;, &#039;b.name&#039;, &#039;c.*&#039;, &#039;d.*&#039;))&lt;br /&gt;
    -&amp;gt;from(&#039;#__content AS a&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;INNER&#039;, &#039;#__users AS b ON (a.created_by = b.id)&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;LEFT&#039;, &#039;#__user_profiles AS c ON (b.id = c.user_id)&#039;)&lt;br /&gt;
    -&amp;gt;join(&#039;RIGHT&#039;, &#039;#__categories AS d ON (a.catid = d.id)&#039;)&lt;br /&gt;
    -&amp;gt;where(&#039;b.username LIKE \&#039;a%\&#039;&#039;)&lt;br /&gt;
    -&amp;gt;order(&#039;a.created DESC&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice how chaining makes the source code much more readable for these longer queries.&lt;br /&gt;
&lt;br /&gt;
====Inserting a Record====&lt;br /&gt;
&lt;br /&gt;
====Updating a Record====&lt;br /&gt;
&lt;br /&gt;
====Deleting a Record====&lt;/div&gt;</summary>
		<author><name>Haydenyoung</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J2.5:Accessing_the_database_using_JDatabase&amp;diff=77627</id>
		<title>J2.5:Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J2.5:Accessing_the_database_using_JDatabase&amp;diff=77627"/>
		<updated>2012-11-19T14:50:12Z</updated>

		<summary type="html">&lt;p&gt;Haydenyoung: /* Selecting Records from Multiple Tables */ A chained multi-table query.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{inuse}}&lt;br /&gt;
&lt;br /&gt;
Joomla provides a sophisticated database abstraction layer to simplify the usage for third party developers. New versions of the Joomla Platform API provide additional functionality which extends the database layer further, and includes features such as connectors to a greater variety of database servers and the query chaining to improve readability of connection code and simplify SQL coding.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
&lt;br /&gt;
Joomla can use different kinds of SQL database systems and run in a variety of environments with different table-prefixes. In addition to these functions, the class automatically creates the database connection. Besides instantiating the object you need just two lines of code to get a result from the database in a variety of formats. Using the Joomla database layer ensures a maximum of compatibility and flexibility for your extension.&lt;br /&gt;
&lt;br /&gt;
===The Connection===&lt;br /&gt;
&lt;br /&gt;
Joomla supports a generic interface for connecting to the database, hiding the intricacies of a specific database server from the Framework developer, thus making Joomla code easier to port from one database to another.&lt;br /&gt;
&lt;br /&gt;
To obtain a database connection, use the JFactory getDbo static method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Alternatively, if your class inherits from the JModel (or a decendent class) getDbo method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = $this-&amp;gt;getDbo();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{JVer|3.0}} users should note that the JModel class has been renamed JModelLegacy and will eventually be deprecated in favour of the class JModelBase. Therefore, any Joomla 3.0 core or extension code should use JFactory::getDbo() to obtain a database connection.&lt;br /&gt;
&lt;br /&gt;
===The Query===&lt;br /&gt;
&lt;br /&gt;
Joomla&#039;s database querying has changed since the new Joomla Framework was introduced  &amp;quot;query chaining&amp;quot; is now the recommended method for building database queries (although string queries are still supported).&lt;br /&gt;
&lt;br /&gt;
Query chaining refers to a method of connecting a number of methods, one after the other, with each method returning an object that can support the next method, improving readability and simplifying code.&lt;br /&gt;
&lt;br /&gt;
To obtain a new instance of the JDatabaseQuery class we use the JDatabaseDriver getQuery method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The JDatabaseDriver::getQuery takes an optional argument, $new, which can be true or false (the default being false).&lt;br /&gt;
&lt;br /&gt;
To query our data source we can call a number of JDatabaseQuery methods; these methods encapsulate the data source&#039;s query language (in most cases SQL), hiding query-specific syntax from the developer and increasing the portability of the developer&#039;s source code. &lt;br /&gt;
&lt;br /&gt;
Some of the more frequently used methods include; select, from, join, where and order. There are also methods such as insert, update and delete for modifying records in the data store. By chaining these and other method calls, you can create almost any query against your data store without compromising portability of your code.&lt;br /&gt;
&lt;br /&gt;
====Selecting Records from a Single Table====&lt;br /&gt;
&lt;br /&gt;
Below is an example of creating a database query using the JDatabaseQuery class. Using the select, from, where and order methods, we can create queries which are flexible, easily readable and portable:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Select all records from the user profile table where key begins with &amp;quot;custom.&amp;quot;.&lt;br /&gt;
// Order it by the ordering field.&lt;br /&gt;
$query-&amp;gt;select(array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;));&lt;br /&gt;
$query-&amp;gt;from(&#039;#__user_profiles&#039;);&lt;br /&gt;
$query-&amp;gt;where(&#039;profile_key LIKE \&#039;custom.%\&#039;&#039;);&lt;br /&gt;
$query-&amp;gt;order(&#039;ordering ASC&#039;);&lt;br /&gt;
&lt;br /&gt;
// Reset the query using our newly populated query object.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
// Load the results as a list of stdClass objects.&lt;br /&gt;
$results = $db-&amp;gt;loadObjectList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The query can also be chained to simplify further:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;select(array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;))&lt;br /&gt;
    -&amp;gt;from(&#039;#__user_profiles&#039;)&lt;br /&gt;
    -&amp;gt;where(&#039;profile_key LIKE \&#039;custom.%\&#039;&#039;)&lt;br /&gt;
    -&amp;gt;order(&#039;ordering ASC&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Chaining can become useful when queries become longer and more complex.&lt;br /&gt;
&lt;br /&gt;
====Selecting Records from Multiple Tables====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Select all articles for users who have a username which starts with &#039;a&#039;.&lt;br /&gt;
// Order it by the created date.&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;select(array(&#039;a.*&#039;, &#039;b.username&#039;, &#039;b.name&#039;))&lt;br /&gt;
    -&amp;gt;from(&#039;#__content AS a&#039;)&lt;br /&gt;
    -&amp;gt;innerJoin(&#039;#__users AS b ON (a.created_by = b.id)&#039;)&lt;br /&gt;
    -&amp;gt;where(&#039;username LIKE \&#039;a%\&#039;&#039;)&lt;br /&gt;
    -&amp;gt;order(&#039;created DESC&#039;);&lt;br /&gt;
&lt;br /&gt;
// Reset the query using our newly populated query object.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
// Load the results as a list of stdClass objects.&lt;br /&gt;
$results = $db-&amp;gt;loadObjectList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Inserting a Record====&lt;br /&gt;
&lt;br /&gt;
====Updating a Record====&lt;br /&gt;
&lt;br /&gt;
====Deleting a Record====&lt;/div&gt;</summary>
		<author><name>Haydenyoung</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J2.5:Accessing_the_database_using_JDatabase&amp;diff=77626</id>
		<title>J2.5:Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J2.5:Accessing_the_database_using_JDatabase&amp;diff=77626"/>
		<updated>2012-11-19T13:54:24Z</updated>

		<summary type="html">&lt;p&gt;Haydenyoung: /* Selecting Records from a Single Table */ Example of chaining.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{inuse}}&lt;br /&gt;
&lt;br /&gt;
Joomla provides a sophisticated database abstraction layer to simplify the usage for third party developers. New versions of the Joomla Platform API provide additional functionality which extends the database layer further, and includes features such as connectors to a greater variety of database servers and the query chaining to improve readability of connection code and simplify SQL coding.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
&lt;br /&gt;
Joomla can use different kinds of SQL database systems and run in a variety of environments with different table-prefixes. In addition to these functions, the class automatically creates the database connection. Besides instantiating the object you need just two lines of code to get a result from the database in a variety of formats. Using the Joomla database layer ensures a maximum of compatibility and flexibility for your extension.&lt;br /&gt;
&lt;br /&gt;
===The Connection===&lt;br /&gt;
&lt;br /&gt;
Joomla supports a generic interface for connecting to the database, hiding the intricacies of a specific database server from the Framework developer, thus making Joomla code easier to port from one database to another.&lt;br /&gt;
&lt;br /&gt;
To obtain a database connection, use the JFactory getDbo static method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Alternatively, if your class inherits from the JModel (or a decendent class) getDbo method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = $this-&amp;gt;getDbo();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{JVer|3.0}} users should note that the JModel class has been renamed JModelLegacy and will eventually be deprecated in favour of the class JModelBase. Therefore, any Joomla 3.0 core or extension code should use JFactory::getDbo() to obtain a database connection.&lt;br /&gt;
&lt;br /&gt;
===The Query===&lt;br /&gt;
&lt;br /&gt;
Joomla&#039;s database querying has changed since the new Joomla Framework was introduced  &amp;quot;query chaining&amp;quot; is now the recommended method for building database queries (although string queries are still supported).&lt;br /&gt;
&lt;br /&gt;
Query chaining refers to a method of connecting a number of methods, one after the other, with each method returning an object that can support the next method, improving readability and simplifying code.&lt;br /&gt;
&lt;br /&gt;
To obtain a new instance of the JDatabaseQuery class we use the JDatabaseDriver getQuery method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The JDatabaseDriver::getQuery takes an optional argument, $new, which can be true or false (the default being false).&lt;br /&gt;
&lt;br /&gt;
To query our data source we can call a number of JDatabaseQuery methods; these methods encapsulate the data source&#039;s query language (in most cases SQL), hiding query-specific syntax from the developer and increasing the portability of the developer&#039;s source code. &lt;br /&gt;
&lt;br /&gt;
Some of the more frequently used methods include; select, from, join, where and order. There are also methods such as insert, update and delete for modifying records in the data store. By chaining these and other method calls, you can create almost any query against your data store without compromising portability of your code.&lt;br /&gt;
&lt;br /&gt;
====Selecting Records from a Single Table====&lt;br /&gt;
&lt;br /&gt;
Below is an example of creating a database query using the JDatabaseQuery class. Using the select, from, where and order methods, we can create queries which are flexible, easily readable and portable:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Select all records from the user profile table where key begins with &amp;quot;custom.&amp;quot;.&lt;br /&gt;
// Order it by the ordering field.&lt;br /&gt;
$query-&amp;gt;select(array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;));&lt;br /&gt;
$query-&amp;gt;from(&#039;#__user_profiles&#039;);&lt;br /&gt;
$query-&amp;gt;where(&#039;profile_key LIKE \&#039;custom.%\&#039;&#039;);&lt;br /&gt;
$query-&amp;gt;order(&#039;ordering ASC&#039;);&lt;br /&gt;
&lt;br /&gt;
// Reset the query using our newly populated query object.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
// Load the results as a list of stdClass objects.&lt;br /&gt;
$results = $db-&amp;gt;loadObjectList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The query can also be chained to simplify further:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query&lt;br /&gt;
    -&amp;gt;select(array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;))&lt;br /&gt;
    -&amp;gt;from(&#039;#__user_profiles&#039;)&lt;br /&gt;
    -&amp;gt;where(&#039;profile_key LIKE \&#039;custom.%\&#039;&#039;)&lt;br /&gt;
    -&amp;gt;order(&#039;ordering ASC&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Chaining can become useful when queries become longer and more complex.&lt;br /&gt;
&lt;br /&gt;
====Selecting Records from Multiple Tables====&lt;br /&gt;
&lt;br /&gt;
====Inserting a Record====&lt;br /&gt;
&lt;br /&gt;
====Updating a Record====&lt;br /&gt;
&lt;br /&gt;
====Deleting a Record====&lt;/div&gt;</summary>
		<author><name>Haydenyoung</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J2.5:Accessing_the_database_using_JDatabase&amp;diff=77625</id>
		<title>J2.5:Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J2.5:Accessing_the_database_using_JDatabase&amp;diff=77625"/>
		<updated>2012-11-19T12:35:14Z</updated>

		<summary type="html">&lt;p&gt;Haydenyoung: /* Selecting Records from a Single Table */ Example of a simple query.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{inuse}}&lt;br /&gt;
&lt;br /&gt;
Joomla provides a sophisticated database abstraction layer to simplify the usage for third party developers. New versions of the Joomla Platform API provide additional functionality which extends the database layer further, and includes features such as connectors to a greater variety of database servers and the query chaining to improve readability of connection code and simplify SQL coding.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
&lt;br /&gt;
Joomla can use different kinds of SQL database systems and run in a variety of environments with different table-prefixes. In addition to these functions, the class automatically creates the database connection. Besides instantiating the object you need just two lines of code to get a result from the database in a variety of formats. Using the Joomla database layer ensures a maximum of compatibility and flexibility for your extension.&lt;br /&gt;
&lt;br /&gt;
===The Connection===&lt;br /&gt;
&lt;br /&gt;
Joomla supports a generic interface for connecting to the database, hiding the intricacies of a specific database server from the Framework developer, thus making Joomla code easier to port from one database to another.&lt;br /&gt;
&lt;br /&gt;
To obtain a database connection, use the JFactory getDbo static method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Alternatively, if your class inherits from the JModel (or a decendent class) getDbo method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = $this-&amp;gt;getDbo();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{JVer|3.0}} users should note that the JModel class has been renamed JModelLegacy and will eventually be deprecated in favour of the class JModelBase. Therefore, any Joomla 3.0 core or extension code should use JFactory::getDbo() to obtain a database connection.&lt;br /&gt;
&lt;br /&gt;
===The Query===&lt;br /&gt;
&lt;br /&gt;
Joomla&#039;s database querying has changed since the new Joomla Framework was introduced  &amp;quot;query chaining&amp;quot; is now the recommended method for building database queries (although string queries are still supported).&lt;br /&gt;
&lt;br /&gt;
Query chaining refers to a method of connecting a number of methods, one after the other, with each method returning an object that can support the next method, improving readability and simplifying code.&lt;br /&gt;
&lt;br /&gt;
To obtain a new instance of the JDatabaseQuery class we use the JDatabaseDriver getQuery method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The JDatabaseDriver::getQuery takes an optional argument, $new, which can be true or false (the default being false).&lt;br /&gt;
&lt;br /&gt;
To query our data source we can call a number of JDatabaseQuery methods; these methods encapsulate the data source&#039;s query language (in most cases SQL), hiding query-specific syntax from the developer and increasing the portability of the developer&#039;s source code. &lt;br /&gt;
&lt;br /&gt;
Some of the more frequently used methods include; select, from, join, where and order. There are also methods such as insert, update and delete for modifying records in the data store. By chaining these and other method calls, you can create almost any query against your data store without compromising portability of your code.&lt;br /&gt;
&lt;br /&gt;
====Selecting Records from a Single Table====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a db connection.&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
// Create a new query object.&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&lt;br /&gt;
// Select all records from the user profile table where key begins with &amp;quot;custom.&amp;quot;.&lt;br /&gt;
// Order it by the ordering field.&lt;br /&gt;
$query-&amp;gt;select(array(&#039;user_id&#039;, &#039;profile_key&#039;, &#039;profile_value&#039;, &#039;ordering&#039;));&lt;br /&gt;
$query-&amp;gt;from(&#039;#__user_profiles&#039;);&lt;br /&gt;
$query-&amp;gt;where(&#039;profile_key LIKE \&#039;custom.%\&#039;&#039;);&lt;br /&gt;
$query-&amp;gt;order(&#039;ordering ASC&#039;);&lt;br /&gt;
&lt;br /&gt;
// Reset the query using our newly populated query object.&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&lt;br /&gt;
// Load the results as a list of stdClass objects.&lt;br /&gt;
$results = $db-&amp;gt;loadObjectList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Selecting Records from Multiple Tables====&lt;br /&gt;
&lt;br /&gt;
====Inserting a Record====&lt;br /&gt;
&lt;br /&gt;
====Updating a Record====&lt;br /&gt;
&lt;br /&gt;
====Deleting a Record====&lt;/div&gt;</summary>
		<author><name>Haydenyoung</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J2.5:Accessing_the_database_using_JDatabase&amp;diff=77602</id>
		<title>J2.5:Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J2.5:Accessing_the_database_using_JDatabase&amp;diff=77602"/>
		<updated>2012-11-16T14:04:00Z</updated>

		<summary type="html">&lt;p&gt;Haydenyoung: /* The Query */ Introduction to query chaining.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{inuse}}&lt;br /&gt;
&lt;br /&gt;
Joomla provides a sophisticated database abstraction layer to simplify the usage for third party developers. New versions of the Joomla Platform API provide additional functionality which extends the database layer further, and includes features such as connectors to a greater variety of database servers and the query chaining to improve readability of connection code and simplify SQL coding.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
&lt;br /&gt;
Joomla can use different kinds of SQL database systems and run in a variety of environments with different table-prefixes. In addition to these functions, the class automatically creates the database connection. Besides instantiating the object you need just two lines of code to get a result from the database in a variety of formats. Using the Joomla database layer ensures a maximum of compatibility and flexibility for your extension.&lt;br /&gt;
&lt;br /&gt;
===The Connection===&lt;br /&gt;
&lt;br /&gt;
Joomla supports a generic interface for connecting to the database, hiding the intricacies of a specific database server from the Framework developer, thus making Joomla code easier to port from one database to another.&lt;br /&gt;
&lt;br /&gt;
To obtain a database connection, use the JFactory getDbo static method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Alternatively, if your class inherits from the JModel (or a decendent class) getDbo method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = $this-&amp;gt;getDbo();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{JVer|3.0}} users should note that the JModel class has been renamed JModelLegacy and will eventually be deprecated in favour of the class JModelBase. Therefore, any Joomla 3.0 core or extension code should use JFactory::getDbo() to obtain a database connection.&lt;br /&gt;
&lt;br /&gt;
===The Query===&lt;br /&gt;
&lt;br /&gt;
Joomla&#039;s database querying has changed since the new Joomla Framework was introduced  &amp;quot;query chaining&amp;quot; is now the recommended method for building database queries (although string queries are still supported).&lt;br /&gt;
&lt;br /&gt;
Query chaining refers to a method of connecting a number of methods, one after the other, with each method returning an object that can support the next method, improving readability and simplifying code.&lt;br /&gt;
&lt;br /&gt;
To obtain a new instance of the JDatabaseQuery class we use the JDatabaseDriver getQuery method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&lt;br /&gt;
$query = $db-&amp;gt;getQuery(true);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The JDatabaseDriver::getQuery takes an optional argument, $new, which can be true or false (the default being false).&lt;br /&gt;
&lt;br /&gt;
To query our data source we can call a number of JDatabaseQuery methods; these methods encapsulate the data source&#039;s query language (in most cases SQL), hiding query-specific syntax from the developer and increasing the portability of the developer&#039;s source code. &lt;br /&gt;
&lt;br /&gt;
Some of the more frequently used methods include; select, from, join, where and order. There are also methods such as insert, update and delete for modifying records in the data store. By chaining these and other method calls, you can create almost any query against your data store without compromising portability of your code.&lt;br /&gt;
&lt;br /&gt;
====Selecting Records from a Single Table====&lt;br /&gt;
&lt;br /&gt;
====Selecting Records from Multiple Tables====&lt;br /&gt;
&lt;br /&gt;
====Inserting a Record====&lt;br /&gt;
&lt;br /&gt;
====Updating a Record====&lt;br /&gt;
&lt;br /&gt;
====Deleting a Record====&lt;/div&gt;</summary>
		<author><name>Haydenyoung</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J2.5:Accessing_the_database_using_JDatabase&amp;diff=77599</id>
		<title>J2.5:Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J2.5:Accessing_the_database_using_JDatabase&amp;diff=77599"/>
		<updated>2012-11-16T06:21:30Z</updated>

		<summary type="html">&lt;p&gt;Haydenyoung: /* Usage */ Connection usage.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{inuse}}&lt;br /&gt;
&lt;br /&gt;
Joomla provides a sophisticated database abstraction layer to simplify the usage for third party developers. New versions of the Joomla Platform API provide additional functionality which extends the database layer further, and includes features such as connectors to a greater variety of database servers and the query chaining to improve readability of connection code and simplify SQL coding.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
&lt;br /&gt;
Joomla can use different kinds of SQL database systems and run in a variety of environments with different table-prefixes. In addition to these functions, the class automatically creates the database connection. Besides instantiating the object you need just two lines of code to get a result from the database in a variety of formats. Using the Joomla database layer ensures a maximum of compatibility and flexibility for your extension.&lt;br /&gt;
&lt;br /&gt;
===The Connection===&lt;br /&gt;
&lt;br /&gt;
Joomla supports a generic interface for connecting to the database, hiding the intricacies of a specific database server from the Framework developer, thus making Joomla code easier to port from one database to another.&lt;br /&gt;
&lt;br /&gt;
To obtain a database connection, use the JFactory getDbo static method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDbo();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Alternatively, if your class inherits from the JModel (or a decendent class) getDbo method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = $this-&amp;gt;getDbo();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{JVer|3.0}} users should note that the JModel class has been renamed JModelLegacy and will eventually be deprecated in favour of the class JModelBase. Therefore, any Joomla 3.0 core or extension code should use JFactory::getDbo() to obtain a database connection.&lt;br /&gt;
&lt;br /&gt;
===The Query===&lt;br /&gt;
&lt;br /&gt;
Joomla&#039;s database querying has changed since the new Joomla Framework was introduced  &amp;quot;query chaining&amp;quot; is now the recommended method for building database queries.&lt;/div&gt;</summary>
		<author><name>Haydenyoung</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Accessing_the_database_using_JDatabase&amp;diff=77598</id>
		<title>Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Accessing_the_database_using_JDatabase&amp;diff=77598"/>
		<updated>2012-11-16T05:17:00Z</updated>

		<summary type="html">&lt;p&gt;Haydenyoung: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{underconstruction}}&lt;br /&gt;
&lt;br /&gt;
{{version/tutor|1.5,2.5,3.0}}{{multiversion|The &#039;&#039;&#039;{{PAGENAME}}&#039;&#039;&#039; Tutorial is available in these versions:-|version|pages=no}}&lt;br /&gt;
&lt;br /&gt;
==Supported Storage Connectors==&lt;br /&gt;
&lt;br /&gt;
The table below outlines the database and storage connectors available for Joomla! as well as which version of Joomla they became available in.&lt;br /&gt;
&lt;br /&gt;
To make a connector available in Joomla&#039;s installer or global configuration manager, you will need to ensure the PHP library is installed (E.g. for PHP5 and MySQL the php5-mysql library would need to be installed).&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Database &lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Joomla Versions&lt;br /&gt;
|-&lt;br /&gt;
| MySQL || {{JVer|1.5}}{{JVer|2.5}}{{JVer|3.0}}&lt;br /&gt;
|-&lt;br /&gt;
| Postgresql || {{JVer|3.0}}&lt;br /&gt;
|-&lt;br /&gt;
| Microsoft SQL Server || {{JVer|2.5}}{{JVer|3.0}}&lt;br /&gt;
|-&lt;br /&gt;
| Microsoft SQL Azure || {{JVer|2.5}}{{JVer|3.0}}&lt;br /&gt;
|-&lt;br /&gt;
| Oracle DB || {{JVer|3.0}}&lt;br /&gt;
|-&lt;br /&gt;
| SQL Lite || {{JVer|3.0}}&lt;br /&gt;
|-&lt;br /&gt;
| PHP Data Objects (PDO)* || {{JVer|3.0}}&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
* PHP Data Objects is a database abstraction layer and is shipped with PHP 5.1+.&lt;/div&gt;</summary>
		<author><name>Haydenyoung</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J2.5:Accessing_the_database_using_JDatabase&amp;diff=77175</id>
		<title>J2.5:Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J2.5:Accessing_the_database_using_JDatabase&amp;diff=77175"/>
		<updated>2012-10-28T03:54:51Z</updated>

		<summary type="html">&lt;p&gt;Haydenyoung: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{inuse}}&lt;br /&gt;
&lt;br /&gt;
Joomla provides a sophisticated database abstraction layer to simplify the usage for third party developers. New versions of the Joomla Platform API provide additional functionality which extends the database layer further, and includes features such as connectors to a greater variety of database servers and the query chaining to improve readability of connection code and simplify SQL coding.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
&lt;br /&gt;
===The Connection===&lt;br /&gt;
&lt;br /&gt;
===The Query===&lt;/div&gt;</summary>
		<author><name>Haydenyoung</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Accessing_the_database_using_JDatabase&amp;diff=77174</id>
		<title>Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Accessing_the_database_using_JDatabase&amp;diff=77174"/>
		<updated>2012-10-28T03:51:27Z</updated>

		<summary type="html">&lt;p&gt;Haydenyoung: /* Supported Storage Connectors */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{inuse}}&lt;br /&gt;
&lt;br /&gt;
{{version/tutor|1.5,2.5,3.0}}{{multiversion|The &#039;&#039;&#039;{{PAGENAME}}&#039;&#039;&#039; Tutorial is available in these versions:-|version|pages=no}}&lt;br /&gt;
&lt;br /&gt;
==Supported Storage Connectors==&lt;br /&gt;
&lt;br /&gt;
The table below outlines the database and storage connectors available for Joomla! as well as which version of Joomla they became available in.&lt;br /&gt;
&lt;br /&gt;
To make a connector available in Joomla&#039;s installer or global configuration manager, you will need to ensure the PHP library is installed (E.g. for PHP5 and MySQL the php5-mysql library would need to be installed).&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Database &lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Joomla Versions&lt;br /&gt;
|-&lt;br /&gt;
| MySQL || {{JVer|1.5}}{{JVer|2.5}}{{JVer|3.0}}&lt;br /&gt;
|-&lt;br /&gt;
| Postgresql || {{JVer|3.0}}&lt;br /&gt;
|-&lt;br /&gt;
| Microsoft SQL Server || {{JVer|2.5}}{{JVer|3.0}}&lt;br /&gt;
|-&lt;br /&gt;
| Microsoft SQL Azure || {{JVer|2.5}}{{JVer|3.0}}&lt;br /&gt;
|-&lt;br /&gt;
| Oracle DB || {{JVer|3.0}}&lt;br /&gt;
|-&lt;br /&gt;
| SQL Lite || {{JVer|3.0}}&lt;br /&gt;
|-&lt;br /&gt;
| PHP Data Objects (PDO)* || {{JVer|3.0}}&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
* PHP Data Objects is a database abstraction layer and is shipped with PHP 5.1+.&lt;/div&gt;</summary>
		<author><name>Haydenyoung</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Accessing_the_database_using_JDatabase&amp;diff=77173</id>
		<title>Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Accessing_the_database_using_JDatabase&amp;diff=77173"/>
		<updated>2012-10-28T03:48:48Z</updated>

		<summary type="html">&lt;p&gt;Haydenyoung: Supported storage connectors.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{inuse}}&lt;br /&gt;
&lt;br /&gt;
{{version/tutor|1.5,2.5,3.0}}{{multiversion|The &#039;&#039;&#039;{{PAGENAME}}&#039;&#039;&#039; Tutorial is available in these versions:-|version|pages=no}}&lt;br /&gt;
&lt;br /&gt;
==Supported Storage Connectors==&lt;br /&gt;
&lt;br /&gt;
The table below outlines the database and storage connectors available for Joomla! as well as which version of Joomla they became available in.&lt;br /&gt;
&lt;br /&gt;
To make a connector available in Joomla&#039;s installer or global configuration manager, you will need to ensure the PHP library is installed (E.g. for PHP5 and MySQL the php5-mysql library would need to be installed).&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Database &lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Joomla Versions&lt;br /&gt;
|-&lt;br /&gt;
| MySQL || {{JVer|1.5}}{{JVer|2.5}}{{JVer|3.0}}&lt;br /&gt;
|-&lt;br /&gt;
| Postgresql || {{JVer|3.0}}&lt;br /&gt;
|-&lt;br /&gt;
| Microsoft SQL Server || {{JVer|2.5}}{{JVer|3.0}}&lt;br /&gt;
|-&lt;br /&gt;
| Microsoft SQL Azure || {{JVer|2.5}}{{JVer|3.0}}&lt;br /&gt;
|-&lt;br /&gt;
| Oracle DB || {{JVer|3.0}}&lt;br /&gt;
|-&lt;br /&gt;
| SQL Lite || {{JVer|3.0}}&lt;br /&gt;
|-&lt;br /&gt;
| PHP Data Objects (PDO) || {{JVer|3.0}}&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Haydenyoung</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J2.5:Accessing_the_database_using_JDatabase&amp;diff=77172</id>
		<title>J2.5:Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J2.5:Accessing_the_database_using_JDatabase&amp;diff=77172"/>
		<updated>2012-10-28T03:17:50Z</updated>

		<summary type="html">&lt;p&gt;Haydenyoung: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{inuse}&lt;br /&gt;
&lt;br /&gt;
Joomla provides a sophisticated database abstraction layer to simplify the usage for third party developers. New versions of the Joomla Platform API provide additional functionality which extends the database layer further, and includes features such as connectors to a greater variety of database servers and the query chaining to improve readability of connection code and simplify SQL coding.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
&lt;br /&gt;
===The Connection===&lt;br /&gt;
&lt;br /&gt;
===The Query===&lt;/div&gt;</summary>
		<author><name>Haydenyoung</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J2.5:Accessing_the_database_using_JDatabase&amp;diff=77171</id>
		<title>J2.5:Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J2.5:Accessing_the_database_using_JDatabase&amp;diff=77171"/>
		<updated>2012-10-28T03:17:33Z</updated>

		<summary type="html">&lt;p&gt;Haydenyoung: New 2.5/3.0 page for database connections and query chaining.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Joomla provides a sophisticated database abstraction layer to simplify the usage for third party developers. New versions of the Joomla Platform API provide additional functionality which extends the database layer further, and includes features such as connectors to a greater variety of database servers and the query chaining to improve readability of connection code and simplify SQL coding.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
&lt;br /&gt;
===The Connection===&lt;br /&gt;
&lt;br /&gt;
===The Query===&lt;/div&gt;</summary>
		<author><name>Haydenyoung</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Accessing_the_database_using_JDatabase&amp;diff=77168</id>
		<title>Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Accessing_the_database_using_JDatabase&amp;diff=77168"/>
		<updated>2012-10-28T02:16:27Z</updated>

		<summary type="html">&lt;p&gt;Haydenyoung: Multiple versions of this tutorial are now available.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{inuse}}&lt;br /&gt;
&lt;br /&gt;
{{version/tutor|1.5,2.5,3.0}}{{multiversion|The &#039;&#039;&#039;{{PAGENAME}}&#039;&#039;&#039; Tutorial is available in these versions:-|version|pages=no}}&lt;/div&gt;</summary>
		<author><name>Haydenyoung</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Accessing_the_database_using_JDatabase&amp;diff=77167</id>
		<title>J1.5:Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Accessing_the_database_using_JDatabase&amp;diff=77167"/>
		<updated>2012-10-28T02:12:16Z</updated>

		<summary type="html">&lt;p&gt;Haydenyoung: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{underconstruction}}&lt;br /&gt;
&lt;br /&gt;
{{version|1.5|test}}Though most of this article still works on 1.5+, some improved ways need to be specified here or in a version specific copy of this article.&amp;lt;hr/&amp;gt;&lt;br /&gt;
Joomla provides a sophisticated database abstraction layer to simplify the usage for third party developers. This guide will help you use this layer.&lt;br /&gt;
&lt;br /&gt;
==Why should I use the Joomla database class?==&lt;br /&gt;
Joomla can use different kinds of SQL database systems and run in a variety of environments with different table-prefixes. In addition to these functions, the class automatically creates the database connection. Besides instantiating the object you need just two lines of code to get a result from the database in a variety of formats. Using the Joomla database layer ensures a maximum of compatibility and flexibility for your extension.&lt;br /&gt;
&lt;br /&gt;
==Preparing the query==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a database object&lt;br /&gt;
$db = JFactory::getDBO();&lt;br /&gt;
&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example_table WHERE id = 999999;&amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First we instantiate the database object; then we prepare the query. You can use the normal SQL syntax. The only thing you have to change is the table prefix. To make this as flexible as possible, Joomla uses a placeholder for the prefix, the &amp;amp;ldquo;#__&amp;amp;rdquo;. In the next step, the $db-&amp;gt;setQuery(), this string is replaced with the correct prefix.&lt;br /&gt;
&lt;br /&gt;
Now, if we don&#039;t want to get information from the database, but rather insert a row into it, we need one more function. Every string value in the SQL syntax should be quoted. For example, MySQL uses backticks &amp;amp;#096;&amp;amp;#096; for names and single quotes &amp;amp;lsquo;&amp;amp;lsquo; for values. Joomla has some functions to do this for us and to ensure code compatibility between different databases. We can pass the names to the function $db-&amp;gt;nameQuote($name) and the values to the function $db-&amp;gt;Quote($value). &lt;br /&gt;
&lt;br /&gt;
A fully quoted query example is:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT * &lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__example_table&#039;).&amp;quot;  &lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;id&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote(&#039;999999&#039;).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Whatever we want to do, we have to set the query with the $db-&amp;gt;setQuery() function. Although you could write the query directly as a parameter for $db-&amp;gt;setQuery(), it&#039;s commonly done by first saving it in a variable, normally $query, and then handing this variable over. This results in clean, readable code.&lt;br /&gt;
&lt;br /&gt;
==== setQuery($query) ====&lt;br /&gt;
The &#039;&#039;setQuery($query)&#039;&#039; method sets up a database query for later execution either by the query() method or one of the Load result methods.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;/* some valid sql string */&amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
The parameter $query must be a valid SQL string. It can either be added as a string parameter or as a variable. Generally a variable is preferred; it leads to more legible code and can help in debugging.&lt;br /&gt;
&lt;br /&gt;
setQuery() also takes three other parameters: $offset, $limit (both used in list pagination) and $prefix, an alternative table prefix. All three variables have default values set and can usually be ignored.&lt;br /&gt;
&lt;br /&gt;
==Executing the Query==&lt;br /&gt;
To execute the query, Joomla provides several functions, which differ in their return value.&lt;br /&gt;
&lt;br /&gt;
=== Basic Query Execution ===&lt;br /&gt;
==== query() ====&lt;br /&gt;
The &#039;&#039;query()&#039;&#039; method is the basic tool for executing SQL queries on a database. In Joomla it is most often used for updating or administering the database simply because the various load methods detailed on this page have the query step built into them.&lt;br /&gt;
&lt;br /&gt;
The syntax is very straightforward:&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$db = JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;/* some valid sql string */&amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;query();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: $query() returns an appropriate database resource if successful, or FALSE if not.&lt;br /&gt;
&lt;br /&gt;
=== Query Execution Information ===&lt;br /&gt;
* getAffectedRows()&lt;br /&gt;
* explain()&lt;br /&gt;
&lt;br /&gt;
==== insertid() ====&lt;br /&gt;
If you insert a record into a table that contains an AUTO_INCREMENT column, you can obtain the value stored into that column by calling the insertid() function&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$query = &amp;quot;INSERT INTO &#039;#__example_table&#039; (&#039;name&#039;,&#039;email&#039;,&#039;username&#039;)&lt;br /&gt;
        VALUES (&#039;John Smith&#039;,&#039;johnsmith@domain.example&#039;,&#039;johnsmith&#039;)&amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$db-&amp;gt;query();&lt;br /&gt;
$user_id = $db-&amp;gt;insertid();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Insert Query Execution ===&lt;br /&gt;
* insertObject()&lt;br /&gt;
&lt;br /&gt;
==Query Results ==&lt;br /&gt;
The database class contains many methods for working with a query&#039;s result set.&lt;br /&gt;
&lt;br /&gt;
=== Single Value Result ===&lt;br /&gt;
==== loadResult() ====&lt;br /&gt;
Use &#039;&#039;&#039;loadResult()&#039;&#039;&#039; when you expect just a single value back from your database query. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- &lt;br /&gt;
| 1 || style=&amp;quot;background:yellow&amp;quot; | John Smith || johnsmith@domain.example || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || Magda Hellman || magda_h@domain.example || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@domain.example || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This is often the result of a &#039;count&#039; query to get a number of records:&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$db = JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT COUNT(*)&lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__my_table&#039;).&amp;quot;&lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;name&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote($value).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$count = $db-&amp;gt;loadResult();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
or where you are just looking for a single field from a single row of the table (or possibly a single field from the first row returned).&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$db = JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT &amp;quot;.$db-&amp;gt;nameQuote(&#039;field_name&#039;).&amp;quot;&lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__my_table&#039;).&amp;quot;&lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;some_name&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote($some_value).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadResult();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Single Row Results ===&lt;br /&gt;
Each of these results functions will return a single record from the database even though there may be several records that meet the criteria that you have set. To get more records you need to call the function again.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 1 || John Smith || johnsmith@domain.example || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || Magda Hellman || magda_h@domain.example || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@domain.example || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadRow() ====&lt;br /&gt;
loadRow() returns an indexed array from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; 1 [1] =&amp;gt; John Smith [2] =&amp;gt; johnsmith@domain.example [3] =&amp;gt; johnsmith ) &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful.&lt;br /&gt;
&lt;br /&gt;
==== loadAssoc() ====&lt;br /&gt;
loadAssoc() returns an associated array from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadAssoc();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@domain.example [username] =&amp;gt; johnsmith )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;name&#039;] // e.g. $row[&#039;name&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful.&lt;br /&gt;
&lt;br /&gt;
==== loadObject() ====&lt;br /&gt;
loadObject returns a PHP object from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadObject();&lt;br /&gt;
print_r($result);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@domain.example [username] =&amp;gt; johnsmith )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$result-&amp;gt;index // e.g. $result-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful.&lt;br /&gt;
&lt;br /&gt;
===Single Column Results ===&lt;br /&gt;
Each of these results functions will return a single column from the database. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- &lt;br /&gt;
| 1 || style=&amp;quot;background:yellow&amp;quot; | John Smith || johnsmith@domain.example || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || style=&amp;quot;background:yellow&amp;quot; | Magda Hellman || magda_h@domain.example || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || style=&amp;quot;background:yellow&amp;quot; | Yvonne de Gaulle || ydg@domain.example || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadResultArray() ====&lt;br /&gt;
loadResultArray() returns an indexed array from a single column in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT name, email, username&lt;br /&gt;
    FROM . . . &amp;quot;;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$column= $db-&amp;gt;loadResultArray();&lt;br /&gt;
print_r($column);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; John Smith [1] =&amp;gt; Magda Hellman [2] =&amp;gt; Yvonne de Gaulle )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$column[&#039;index&#039;] // e.g. $column[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
# loadResultArray() is equivalent to loadResultArray(0).&lt;br /&gt;
&lt;br /&gt;
==== loadResultArray($index) ====&lt;br /&gt;
loadResultArray($index) returns an indexed array from a single column in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT name, email, username&lt;br /&gt;
    FROM . . . &amp;quot;;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$column= $db-&amp;gt;loadResultArray(1);&lt;br /&gt;
print_r($column);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; johnsmith@domain.example [1] =&amp;gt; magda_h@domain.example [2] =&amp;gt; ydg@domain.example )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$column[&#039;index&#039;] // e.g. $column[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
loadResultArray($index) allows you to iterate through a series of columns in the results&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
for ( $i = 0; $i &amp;lt;= 2; $i++ ) {&lt;br /&gt;
  $column= $db-&amp;gt;loadResultArray($i);&lt;br /&gt;
  print_r($column);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; John Smith [1] =&amp;gt; Magda Hellman [2] =&amp;gt; Yvonne de Gaulle )&lt;br /&gt;
Array ( [0] =&amp;gt; johnsmith@domain.example [1] =&amp;gt; magda_h@domain.example [2] =&amp;gt; ydg@domain.example )&lt;br /&gt;
Array ( [0] =&amp;gt; johnsmith [1] =&amp;gt; magdah [2] =&amp;gt; ydegaulle )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
&lt;br /&gt;
=== Multi-Row Results ===&lt;br /&gt;
Each of these results functions will return multiple records from the database. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 1 || John Smith || johnsmith@domain.example || johnsmith&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 2 || Magda Hellman || magda_h@domain.example || magdah&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@domain.example || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadRowList() ====&lt;br /&gt;
loadRowList() returns an indexed array of indexed arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRowList();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; Array ( [0] =&amp;gt; 1 [1] =&amp;gt; John Smith [2] =&amp;gt; johnsmith@domain.example [3] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; Array ( [0] =&amp;gt; 2 [1] =&amp;gt; Magda Hellman [2] =&amp;gt; magda_h@domain.example [3] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; Array ( [0] =&amp;gt; 3 [1] =&amp;gt; Yvonne de Gaulle [2] =&amp;gt; ydg@domain.example [3] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;][&#039;index&#039;] // e.g. $row[&#039;2&#039;][&#039;3&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
&lt;br /&gt;
==== loadAssocList() ====&lt;br /&gt;
loadAssocList() returns an indexed array of associated arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadAssocList();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@domain.example [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; Array ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman [email] =&amp;gt; magda_h@domain.example [username] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; Array ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle [email] =&amp;gt; ydg@domain.example [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
) &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;][&#039;column_name&#039;] // e.g. $row[&#039;2&#039;][&#039;email&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== loadAssocList($key) ====&lt;br /&gt;
loadAssocList(&#039;key&#039;) returns an associated array - indexed on &#039;key&#039; - of associated arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadAssocList(&#039;username&#039;);&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[johnsmith] =&amp;gt; Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@domain.example [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[magdah] =&amp;gt; Array ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman [email] =&amp;gt; magda_h@domain.example [username] =&amp;gt; magdah ) &lt;br /&gt;
[ydegaulle] =&amp;gt; Array ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle [email] =&amp;gt; ydg@domain.example [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;] // e.g. $row[&#039;johnsmith&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;][&#039;column_name&#039;] // e.g. $row[&#039;johnsmith&#039;][&#039;email&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: Key must be a valid column name from the table; it does not have to be an Index or a Primary Key. But if it does not have a unique value you may not be able to retrieve results reliably.&lt;br /&gt;
&lt;br /&gt;
==== loadObjectList() ====&lt;br /&gt;
loadObjectList() returns an indexed array of PHP objects from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadObjectList();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith &lt;br /&gt;
    [email] =&amp;gt; johnsmith@domain.example [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; stdClass Object ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman &lt;br /&gt;
    [email] =&amp;gt; magda_h@domain.example [username] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; stdClass Object ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle &lt;br /&gt;
    [email] =&amp;gt; ydg@domain.example [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;]-&amp;gt;name // e.g. $row[&#039;2&#039;]-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== loadObjectList(&#039;key&#039;) ====&lt;br /&gt;
loadObjectList($key) returns an associated array - indexed on &#039;key&#039; - of objects from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadObjectList(&#039;username&#039;);&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[johnsmith] =&amp;gt; stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith &lt;br /&gt;
    [email] =&amp;gt; johnsmith@domain.example [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[magdah] =&amp;gt; stdClass Object ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman &lt;br /&gt;
    [email] =&amp;gt; magda_h@domain.example [username] =&amp;gt; magdah ) &lt;br /&gt;
[ydegaulle] =&amp;gt; stdClass Object ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle &lt;br /&gt;
    [email] =&amp;gt; ydg@domain.example [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;] // e.g. $row[&#039;johnsmith&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;]-&amp;gt;column_name // e.g. $row[&#039;johnsmith&#039;]-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: Key must be a valid column name from the table; it does not have to be an Index or a Primary Key. But if it does not have a unique value you may not be able to retrieve results reliably.&lt;br /&gt;
&lt;br /&gt;
=== Miscellaneous Result Set Methods ===&lt;br /&gt;
==== getNumRows() ====&lt;br /&gt;
getNumRows() will return the number of result rows found by the last query and waiting to be read. To get a result from getNumRows() you have to run it &#039;&#039;&#039;after&#039;&#039;&#039; the query and &#039;&#039;&#039;before&#039;&#039;&#039; you have retrieved any results.  &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$db-&amp;gt;query();&lt;br /&gt;
$num_rows = $db-&amp;gt;getNumRows();&lt;br /&gt;
print_r($num_rows);&lt;br /&gt;
$result = $db-&amp;gt;loadRowList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will return &amp;lt;pre&amp;gt;3&amp;lt;/pre&amp;gt;&lt;br /&gt;
Note: if you run getNumRows() after loadRowList() - or any other retrieval method - you may get a PHP Warning:&lt;br /&gt;
&amp;lt;pre&amp;gt;Warning: mysql_num_rows(): 80 is not a valid MySQL result resource &lt;br /&gt;
in D:\xampp\htdocs\joomla1.5a\libraries\joomla\database\database\mysql.php on line 344&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Tips, Tricks &amp;amp; FAQ==&lt;br /&gt;
===Subqueries===&lt;br /&gt;
Subqueries should be written as follows:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;SQL&amp;quot;&amp;gt;&lt;br /&gt;
SELECT * FROM #__example WHERE id IN (SELECT id FROM #__example2);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
These kinds of queries are supported since MySQL 4.1. If this method fails, you can split the query into two as demonstrated below. Please note that this will (often unnecessarily) increase the load on the database server.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query = &amp;quot;SELECT id FROM #__example2&amp;quot;;&lt;br /&gt;
$database-&amp;gt;setQuery($query);&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example WHERE id IN (&amp;quot;. implode(&amp;quot;,&amp;quot;, $database-&amp;gt;loadResultArray()) .&amp;quot;)&amp;quot;;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Using MySQL User-Defined Variables===&lt;br /&gt;
MySQL User-Defined Variables are created and maintained for the lifespan of a connection. In Joomla terms this is usually the lifespan of a page request. They can be used in consecutive queries, retaining their value, as long as each query is inside the same connection lifespan.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$database = JFactory::getDBO();&lt;br /&gt;
$database-&amp;gt;setQuery(&amp;quot;SET @num := 0&amp;quot;);&lt;br /&gt;
$database-&amp;gt;query();&lt;br /&gt;
$database-&amp;gt;setQuery(&amp;quot;SET @num := @num + 5&amp;quot;);&lt;br /&gt;
$database-&amp;gt;query();&lt;br /&gt;
$database-&amp;gt;setQuery(&amp;quot;SELECT @num&amp;quot;);&lt;br /&gt;
$result = $localDB-&amp;gt;loadResult();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
The first query defines the MySQL User-Defined Variable, the second one increments it by 5, the third one retrieves it&#039;s value and will in this case return a value of 5.&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;br/&amp;gt;&lt;br /&gt;
The fact that they retain their value can be quite useful, however there is also a little risk. If you use the same User-Defined Variable, in this case @num, in another query, make sure you reset it first. If that other query runs within the same connection lifespan, @num will have remembered its value of 5. You may expect it to start at null or 0.&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;br/&amp;gt;&lt;br /&gt;
For details on working with MySQL&#039;s User-Defined Variables, please refer to the MySQL documentation at [http://dev.mysql.com/doc/ http://dev.mysql.com/doc/]&lt;br /&gt;
&lt;br /&gt;
===Developer-Friendly Tips===&lt;br /&gt;
Here is a quick way to do four developer-friendly things at once:&lt;br /&gt;
* Use a simple constant as an SQL seperator (which can probably be used in many queries).&lt;br /&gt;
* Make your SQL-in-PHP code easy to read (for yourself and possibly other developers later on).&lt;br /&gt;
* Give an error inside your (component-) content without really setting debugging on.&lt;br /&gt;
* Have a visibly nice SQL by splitting SQL groups with linebreaks in your error.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDBO();&lt;br /&gt;
$jAp = JFactory::getApplication();&lt;br /&gt;
    //We define a linebreak constant&lt;br /&gt;
define(&#039;L&#039;, chr(10));&lt;br /&gt;
    //Here is the most magic&lt;br /&gt;
$db-&amp;gt;setQuery(&lt;br /&gt;
	&#039;SELECT * FROM #__table&#039;.L.&lt;br /&gt;
	&#039;WHERE something=&amp;quot;something else&amp;quot;)&#039;.L.&lt;br /&gt;
	&#039;ORDER BY date desc&#039;&lt;br /&gt;
); &lt;br /&gt;
$db-&amp;gt;query();&lt;br /&gt;
    //display and convert to HTML when SQL error&lt;br /&gt;
if (is_null($posts=$db-&amp;gt;loadRowList())) {$jAp-&amp;gt;enqueueMessage(nl2br($db-&amp;gt;getErrorMsg()),&#039;error&#039;); return;} &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;noinclude&amp;gt;[[Category:Tutorials]][[Category:Development]][[Category:Database]]&amp;lt;/noinclude&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===See your resulting query===&lt;br /&gt;
when building an extension, if you want to see the resulting query you can use:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
echo nl2br(str_replace(&#039;#__&#039;,&#039;jos_&#039;,$query));die; //Where &#039;jos_&#039; is your database prefix &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Haydenyoung</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Accessing_the_database_using_JDatabase&amp;diff=77166</id>
		<title>J1.5:Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Accessing_the_database_using_JDatabase&amp;diff=77166"/>
		<updated>2012-10-28T02:07:24Z</updated>

		<summary type="html">&lt;p&gt;Haydenyoung: Re-organizing pages into the various version implementations. This article will replace the existing Accessing the database using JDatabase article.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{version|1.5|test}}Though most of this article still works on 1.5+, some improved ways need to be specified here or in a version specific copy of this article.&amp;lt;hr/&amp;gt;&lt;br /&gt;
Joomla provides a sophisticated database abstraction layer to simplify the usage for third party developers. This guide will help you use this layer.&lt;br /&gt;
&lt;br /&gt;
==Why should I use the Joomla database class?==&lt;br /&gt;
Joomla can use different kinds of SQL database systems and run in a variety of environments with different table-prefixes. In addition to these functions, the class automatically creates the database connection. Besides instantiating the object you need just two lines of code to get a result from the database in a variety of formats. Using the Joomla database layer ensures a maximum of compatibility and flexibility for your extension.&lt;br /&gt;
&lt;br /&gt;
==Preparing the query==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a database object&lt;br /&gt;
$db = JFactory::getDBO();&lt;br /&gt;
&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example_table WHERE id = 999999;&amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First we instantiate the database object; then we prepare the query. You can use the normal SQL syntax. The only thing you have to change is the table prefix. To make this as flexible as possible, Joomla uses a placeholder for the prefix, the &amp;amp;ldquo;#__&amp;amp;rdquo;. In the next step, the $db-&amp;gt;setQuery(), this string is replaced with the correct prefix.&lt;br /&gt;
&lt;br /&gt;
Now, if we don&#039;t want to get information from the database, but rather insert a row into it, we need one more function. Every string value in the SQL syntax should be quoted. For example, MySQL uses backticks &amp;amp;#096;&amp;amp;#096; for names and single quotes &amp;amp;lsquo;&amp;amp;lsquo; for values. Joomla has some functions to do this for us and to ensure code compatibility between different databases. We can pass the names to the function $db-&amp;gt;nameQuote($name) and the values to the function $db-&amp;gt;Quote($value). &lt;br /&gt;
&lt;br /&gt;
A fully quoted query example is:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT * &lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__example_table&#039;).&amp;quot;  &lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;id&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote(&#039;999999&#039;).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Whatever we want to do, we have to set the query with the $db-&amp;gt;setQuery() function. Although you could write the query directly as a parameter for $db-&amp;gt;setQuery(), it&#039;s commonly done by first saving it in a variable, normally $query, and then handing this variable over. This results in clean, readable code.&lt;br /&gt;
&lt;br /&gt;
==== setQuery($query) ====&lt;br /&gt;
The &#039;&#039;setQuery($query)&#039;&#039; method sets up a database query for later execution either by the query() method or one of the Load result methods.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;/* some valid sql string */&amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
The parameter $query must be a valid SQL string. It can either be added as a string parameter or as a variable. Generally a variable is preferred; it leads to more legible code and can help in debugging.&lt;br /&gt;
&lt;br /&gt;
setQuery() also takes three other parameters: $offset, $limit (both used in list pagination) and $prefix, an alternative table prefix. All three variables have default values set and can usually be ignored.&lt;br /&gt;
&lt;br /&gt;
==Executing the Query==&lt;br /&gt;
To execute the query, Joomla provides several functions, which differ in their return value.&lt;br /&gt;
&lt;br /&gt;
=== Basic Query Execution ===&lt;br /&gt;
==== query() ====&lt;br /&gt;
The &#039;&#039;query()&#039;&#039; method is the basic tool for executing SQL queries on a database. In Joomla it is most often used for updating or administering the database simply because the various load methods detailed on this page have the query step built into them.&lt;br /&gt;
&lt;br /&gt;
The syntax is very straightforward:&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$db = JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;/* some valid sql string */&amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;query();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: $query() returns an appropriate database resource if successful, or FALSE if not.&lt;br /&gt;
&lt;br /&gt;
=== Query Execution Information ===&lt;br /&gt;
* getAffectedRows()&lt;br /&gt;
* explain()&lt;br /&gt;
&lt;br /&gt;
==== insertid() ====&lt;br /&gt;
If you insert a record into a table that contains an AUTO_INCREMENT column, you can obtain the value stored into that column by calling the insertid() function&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$query = &amp;quot;INSERT INTO &#039;#__example_table&#039; (&#039;name&#039;,&#039;email&#039;,&#039;username&#039;)&lt;br /&gt;
        VALUES (&#039;John Smith&#039;,&#039;johnsmith@domain.example&#039;,&#039;johnsmith&#039;)&amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$db-&amp;gt;query();&lt;br /&gt;
$user_id = $db-&amp;gt;insertid();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Insert Query Execution ===&lt;br /&gt;
* insertObject()&lt;br /&gt;
&lt;br /&gt;
==Query Results ==&lt;br /&gt;
The database class contains many methods for working with a query&#039;s result set.&lt;br /&gt;
&lt;br /&gt;
=== Single Value Result ===&lt;br /&gt;
==== loadResult() ====&lt;br /&gt;
Use &#039;&#039;&#039;loadResult()&#039;&#039;&#039; when you expect just a single value back from your database query. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- &lt;br /&gt;
| 1 || style=&amp;quot;background:yellow&amp;quot; | John Smith || johnsmith@domain.example || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || Magda Hellman || magda_h@domain.example || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@domain.example || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This is often the result of a &#039;count&#039; query to get a number of records:&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$db = JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT COUNT(*)&lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__my_table&#039;).&amp;quot;&lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;name&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote($value).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$count = $db-&amp;gt;loadResult();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
or where you are just looking for a single field from a single row of the table (or possibly a single field from the first row returned).&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$db = JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT &amp;quot;.$db-&amp;gt;nameQuote(&#039;field_name&#039;).&amp;quot;&lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__my_table&#039;).&amp;quot;&lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;some_name&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote($some_value).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadResult();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Single Row Results ===&lt;br /&gt;
Each of these results functions will return a single record from the database even though there may be several records that meet the criteria that you have set. To get more records you need to call the function again.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 1 || John Smith || johnsmith@domain.example || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || Magda Hellman || magda_h@domain.example || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@domain.example || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadRow() ====&lt;br /&gt;
loadRow() returns an indexed array from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; 1 [1] =&amp;gt; John Smith [2] =&amp;gt; johnsmith@domain.example [3] =&amp;gt; johnsmith ) &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful.&lt;br /&gt;
&lt;br /&gt;
==== loadAssoc() ====&lt;br /&gt;
loadAssoc() returns an associated array from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadAssoc();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@domain.example [username] =&amp;gt; johnsmith )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;name&#039;] // e.g. $row[&#039;name&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful.&lt;br /&gt;
&lt;br /&gt;
==== loadObject() ====&lt;br /&gt;
loadObject returns a PHP object from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadObject();&lt;br /&gt;
print_r($result);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@domain.example [username] =&amp;gt; johnsmith )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$result-&amp;gt;index // e.g. $result-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful.&lt;br /&gt;
&lt;br /&gt;
===Single Column Results ===&lt;br /&gt;
Each of these results functions will return a single column from the database. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- &lt;br /&gt;
| 1 || style=&amp;quot;background:yellow&amp;quot; | John Smith || johnsmith@domain.example || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || style=&amp;quot;background:yellow&amp;quot; | Magda Hellman || magda_h@domain.example || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || style=&amp;quot;background:yellow&amp;quot; | Yvonne de Gaulle || ydg@domain.example || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadResultArray() ====&lt;br /&gt;
loadResultArray() returns an indexed array from a single column in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT name, email, username&lt;br /&gt;
    FROM . . . &amp;quot;;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$column= $db-&amp;gt;loadResultArray();&lt;br /&gt;
print_r($column);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; John Smith [1] =&amp;gt; Magda Hellman [2] =&amp;gt; Yvonne de Gaulle )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$column[&#039;index&#039;] // e.g. $column[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
# loadResultArray() is equivalent to loadResultArray(0).&lt;br /&gt;
&lt;br /&gt;
==== loadResultArray($index) ====&lt;br /&gt;
loadResultArray($index) returns an indexed array from a single column in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT name, email, username&lt;br /&gt;
    FROM . . . &amp;quot;;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$column= $db-&amp;gt;loadResultArray(1);&lt;br /&gt;
print_r($column);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; johnsmith@domain.example [1] =&amp;gt; magda_h@domain.example [2] =&amp;gt; ydg@domain.example )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$column[&#039;index&#039;] // e.g. $column[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
loadResultArray($index) allows you to iterate through a series of columns in the results&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
for ( $i = 0; $i &amp;lt;= 2; $i++ ) {&lt;br /&gt;
  $column= $db-&amp;gt;loadResultArray($i);&lt;br /&gt;
  print_r($column);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; John Smith [1] =&amp;gt; Magda Hellman [2] =&amp;gt; Yvonne de Gaulle )&lt;br /&gt;
Array ( [0] =&amp;gt; johnsmith@domain.example [1] =&amp;gt; magda_h@domain.example [2] =&amp;gt; ydg@domain.example )&lt;br /&gt;
Array ( [0] =&amp;gt; johnsmith [1] =&amp;gt; magdah [2] =&amp;gt; ydegaulle )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
&lt;br /&gt;
=== Multi-Row Results ===&lt;br /&gt;
Each of these results functions will return multiple records from the database. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 1 || John Smith || johnsmith@domain.example || johnsmith&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 2 || Magda Hellman || magda_h@domain.example || magdah&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@domain.example || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadRowList() ====&lt;br /&gt;
loadRowList() returns an indexed array of indexed arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRowList();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; Array ( [0] =&amp;gt; 1 [1] =&amp;gt; John Smith [2] =&amp;gt; johnsmith@domain.example [3] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; Array ( [0] =&amp;gt; 2 [1] =&amp;gt; Magda Hellman [2] =&amp;gt; magda_h@domain.example [3] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; Array ( [0] =&amp;gt; 3 [1] =&amp;gt; Yvonne de Gaulle [2] =&amp;gt; ydg@domain.example [3] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;][&#039;index&#039;] // e.g. $row[&#039;2&#039;][&#039;3&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
&lt;br /&gt;
==== loadAssocList() ====&lt;br /&gt;
loadAssocList() returns an indexed array of associated arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadAssocList();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@domain.example [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; Array ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman [email] =&amp;gt; magda_h@domain.example [username] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; Array ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle [email] =&amp;gt; ydg@domain.example [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
) &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;][&#039;column_name&#039;] // e.g. $row[&#039;2&#039;][&#039;email&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== loadAssocList($key) ====&lt;br /&gt;
loadAssocList(&#039;key&#039;) returns an associated array - indexed on &#039;key&#039; - of associated arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadAssocList(&#039;username&#039;);&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[johnsmith] =&amp;gt; Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@domain.example [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[magdah] =&amp;gt; Array ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman [email] =&amp;gt; magda_h@domain.example [username] =&amp;gt; magdah ) &lt;br /&gt;
[ydegaulle] =&amp;gt; Array ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle [email] =&amp;gt; ydg@domain.example [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;] // e.g. $row[&#039;johnsmith&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;][&#039;column_name&#039;] // e.g. $row[&#039;johnsmith&#039;][&#039;email&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: Key must be a valid column name from the table; it does not have to be an Index or a Primary Key. But if it does not have a unique value you may not be able to retrieve results reliably.&lt;br /&gt;
&lt;br /&gt;
==== loadObjectList() ====&lt;br /&gt;
loadObjectList() returns an indexed array of PHP objects from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadObjectList();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith &lt;br /&gt;
    [email] =&amp;gt; johnsmith@domain.example [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; stdClass Object ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman &lt;br /&gt;
    [email] =&amp;gt; magda_h@domain.example [username] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; stdClass Object ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle &lt;br /&gt;
    [email] =&amp;gt; ydg@domain.example [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;]-&amp;gt;name // e.g. $row[&#039;2&#039;]-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== loadObjectList(&#039;key&#039;) ====&lt;br /&gt;
loadObjectList($key) returns an associated array - indexed on &#039;key&#039; - of objects from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadObjectList(&#039;username&#039;);&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[johnsmith] =&amp;gt; stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith &lt;br /&gt;
    [email] =&amp;gt; johnsmith@domain.example [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[magdah] =&amp;gt; stdClass Object ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman &lt;br /&gt;
    [email] =&amp;gt; magda_h@domain.example [username] =&amp;gt; magdah ) &lt;br /&gt;
[ydegaulle] =&amp;gt; stdClass Object ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle &lt;br /&gt;
    [email] =&amp;gt; ydg@domain.example [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;] // e.g. $row[&#039;johnsmith&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;]-&amp;gt;column_name // e.g. $row[&#039;johnsmith&#039;]-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: Key must be a valid column name from the table; it does not have to be an Index or a Primary Key. But if it does not have a unique value you may not be able to retrieve results reliably.&lt;br /&gt;
&lt;br /&gt;
=== Miscellaneous Result Set Methods ===&lt;br /&gt;
==== getNumRows() ====&lt;br /&gt;
getNumRows() will return the number of result rows found by the last query and waiting to be read. To get a result from getNumRows() you have to run it &#039;&#039;&#039;after&#039;&#039;&#039; the query and &#039;&#039;&#039;before&#039;&#039;&#039; you have retrieved any results.  &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$db-&amp;gt;query();&lt;br /&gt;
$num_rows = $db-&amp;gt;getNumRows();&lt;br /&gt;
print_r($num_rows);&lt;br /&gt;
$result = $db-&amp;gt;loadRowList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will return &amp;lt;pre&amp;gt;3&amp;lt;/pre&amp;gt;&lt;br /&gt;
Note: if you run getNumRows() after loadRowList() - or any other retrieval method - you may get a PHP Warning:&lt;br /&gt;
&amp;lt;pre&amp;gt;Warning: mysql_num_rows(): 80 is not a valid MySQL result resource &lt;br /&gt;
in D:\xampp\htdocs\joomla1.5a\libraries\joomla\database\database\mysql.php on line 344&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Tips, Tricks &amp;amp; FAQ==&lt;br /&gt;
===Subqueries===&lt;br /&gt;
Subqueries should be written as follows:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;SQL&amp;quot;&amp;gt;&lt;br /&gt;
SELECT * FROM #__example WHERE id IN (SELECT id FROM #__example2);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
These kinds of queries are supported since MySQL 4.1. If this method fails, you can split the query into two as demonstrated below. Please note that this will (often unnecessarily) increase the load on the database server.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query = &amp;quot;SELECT id FROM #__example2&amp;quot;;&lt;br /&gt;
$database-&amp;gt;setQuery($query);&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example WHERE id IN (&amp;quot;. implode(&amp;quot;,&amp;quot;, $database-&amp;gt;loadResultArray()) .&amp;quot;)&amp;quot;;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Using MySQL User-Defined Variables===&lt;br /&gt;
MySQL User-Defined Variables are created and maintained for the lifespan of a connection. In Joomla terms this is usually the lifespan of a page request. They can be used in consecutive queries, retaining their value, as long as each query is inside the same connection lifespan.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$database = JFactory::getDBO();&lt;br /&gt;
$database-&amp;gt;setQuery(&amp;quot;SET @num := 0&amp;quot;);&lt;br /&gt;
$database-&amp;gt;query();&lt;br /&gt;
$database-&amp;gt;setQuery(&amp;quot;SET @num := @num + 5&amp;quot;);&lt;br /&gt;
$database-&amp;gt;query();&lt;br /&gt;
$database-&amp;gt;setQuery(&amp;quot;SELECT @num&amp;quot;);&lt;br /&gt;
$result = $localDB-&amp;gt;loadResult();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
The first query defines the MySQL User-Defined Variable, the second one increments it by 5, the third one retrieves it&#039;s value and will in this case return a value of 5.&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;br/&amp;gt;&lt;br /&gt;
The fact that they retain their value can be quite useful, however there is also a little risk. If you use the same User-Defined Variable, in this case @num, in another query, make sure you reset it first. If that other query runs within the same connection lifespan, @num will have remembered its value of 5. You may expect it to start at null or 0.&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;br/&amp;gt;&lt;br /&gt;
For details on working with MySQL&#039;s User-Defined Variables, please refer to the MySQL documentation at [http://dev.mysql.com/doc/ http://dev.mysql.com/doc/]&lt;br /&gt;
&lt;br /&gt;
===Developer-Friendly Tips===&lt;br /&gt;
Here is a quick way to do four developer-friendly things at once:&lt;br /&gt;
* Use a simple constant as an SQL seperator (which can probably be used in many queries).&lt;br /&gt;
* Make your SQL-in-PHP code easy to read (for yourself and possibly other developers later on).&lt;br /&gt;
* Give an error inside your (component-) content without really setting debugging on.&lt;br /&gt;
* Have a visibly nice SQL by splitting SQL groups with linebreaks in your error.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDBO();&lt;br /&gt;
$jAp = JFactory::getApplication();&lt;br /&gt;
    //We define a linebreak constant&lt;br /&gt;
define(&#039;L&#039;, chr(10));&lt;br /&gt;
    //Here is the most magic&lt;br /&gt;
$db-&amp;gt;setQuery(&lt;br /&gt;
	&#039;SELECT * FROM #__table&#039;.L.&lt;br /&gt;
	&#039;WHERE something=&amp;quot;something else&amp;quot;)&#039;.L.&lt;br /&gt;
	&#039;ORDER BY date desc&#039;&lt;br /&gt;
); &lt;br /&gt;
$db-&amp;gt;query();&lt;br /&gt;
    //display and convert to HTML when SQL error&lt;br /&gt;
if (is_null($posts=$db-&amp;gt;loadRowList())) {$jAp-&amp;gt;enqueueMessage(nl2br($db-&amp;gt;getErrorMsg()),&#039;error&#039;); return;} &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;noinclude&amp;gt;[[Category:Tutorials]][[Category:Development]][[Category:Database]]&amp;lt;/noinclude&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===See your resulting query===&lt;br /&gt;
when building an extension, if you want to see the resulting query you can use:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
echo nl2br(str_replace(&#039;#__&#039;,&#039;jos_&#039;,$query));die; //Where &#039;jos_&#039; is your database prefix &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Haydenyoung</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Adding_JavaScript&amp;diff=77164</id>
		<title>Adding JavaScript</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Adding_JavaScript&amp;diff=77164"/>
		<updated>2012-10-28T02:04:22Z</updated>

		<summary type="html">&lt;p&gt;Haydenyoung: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{version/tutor|1.5,2.5,3.0}}&lt;br /&gt;
&lt;br /&gt;
JavaScript (also known as ECMAScript) is a scripting language primarily used in client-side web site development to extend and enhance an end-user&#039;s experience. Joomla provides developers with easy-to-use mechanisms to include JavaScript in their extensions using existing API methods.&lt;br /&gt;
&lt;br /&gt;
There are a number of methods for including JavaScript in your Joomla extensions; some of these are described below.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
&lt;br /&gt;
There are three methods for embedding JavaScript into your code using the Joomla API; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration], [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] and [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script]. These methods should be called either in your component&#039;s View class (&amp;lt;yourcomponent&amp;gt;/views/&amp;lt;yourview&amp;gt;/view.html.php) or template script (&amp;lt;yourcomponent&amp;gt;/views/&amp;lt;yourview&amp;gt;/tmpl/&amp;lt;yourtemplate&amp;gt;.php or in the case of a module, in its template script (&amp;lt;yourmodule&amp;gt;/tmpl/&amp;lt;yourtemplate&amp;gt;.php). &lt;br /&gt;
&lt;br /&gt;
===Inline JavaScript===&lt;br /&gt;
&lt;br /&gt;
Blocks of JavaScript code can be declared directly within a component or module&#039;s display template using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html JDocument] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration addScriptDeclaration] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$document = &amp;amp;JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addScriptDeclaration(&#039;&lt;br /&gt;
    window.event(&amp;quot;domready&amp;quot;, function() {&lt;br /&gt;
        alert(&amp;quot;An inline JavaScript Declaration&amp;quot;);&lt;br /&gt;
    });&lt;br /&gt;
&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===External JavaScript===&lt;br /&gt;
&lt;br /&gt;
Alternatively, you may wish to separate your JavaScript into a separate file. Separating your JavaScript into an external file can make your template code easier to read especially if the JavaScript is lengthy or complex.&lt;br /&gt;
&lt;br /&gt;
There are two ways to include a JavaScript file using the Joomla API. The first involves using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html JDocument] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript addScript] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$document = &amp;amp;JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addScript(&#039;/media/system/js/sample.js&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The second uses the [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html JHTML] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// Add the path parameter if the path is different than &#039;media/system/js/&#039;&lt;br /&gt;
JHTML::script(&#039;sample.js&#039;, &#039;templates/custom/js/&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
The Joomla API&#039;s [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration], [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] and [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] methods embed JavaScript into Joomla&#039;s index.php via the jdoc head tag:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;jdoc:include type=&amp;quot;head&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] or [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] methods to embed JavaScript includes would result in the index.php rendering the following HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;script type=&amp;quot;text/javaScript&amp;quot; src=&amp;quot;/media/system/js/sample.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Calling the class method [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration] would render the following HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;script type=&amp;quot;text/javaScript&amp;quot;&amp;gt;&lt;br /&gt;
window.addEvent(&amp;quot;domready&amp;quot;, function() {&lt;br /&gt;
    alert(&amp;quot;Embedded block of JS here&amp;quot;);&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/script&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using these methods is highly recommended as it clearly differentiates another scripting language (JavaScript) from the main PHP code, ensures all JavaScript is correctly embedded between the &amp;lt;head&amp;gt;&amp;lt;/head&amp;gt; tags and, in the case of JDocument::addScript and JHTML::script ensures that a JavaScript file is included only once (I.e. there is no .js file duplication).&lt;br /&gt;
&lt;br /&gt;
== Using a JavaScript Framework ==&lt;br /&gt;
&lt;br /&gt;
A Javascript framework provides developers with generic functionality for handling various coding tasks in a familiar, consistent and platform-independent way. A framework enables the developer to forget about the intricacies of implementing a certain function in different web browsers and focus on the requirements of the software.&lt;br /&gt;
&lt;br /&gt;
Two Javascript frameworks are provided as part of Joomla 3.0; JQuery and Mootools. JQuery is a newly introduced framework which integrates with Joomla&#039;s new Bootstrap HTML framework; Mootools is Joomla&#039;s legacy Javascript library which is now superseded by JQuery and is included for backwards compatibility with 3rd party extensions.&lt;br /&gt;
&lt;br /&gt;
In nearly all cases you should use a framework when developing Javascript in your extensions or templates and including one is very simple with Joomla&#039;s API.&lt;br /&gt;
&lt;br /&gt;
=== JQuery ===&lt;br /&gt;
&lt;br /&gt;
As of Joomla 3.0 it is highly recommended you use the JQuery framework for all your Javascript as it is loaded by nearly every template which uses the Bootstrap HTML/CSS framework. In those few instances where JQuery is not available, you can easily include the framework using the JHTMLJQuery class&#039; framework method.&lt;br /&gt;
&lt;br /&gt;
To include the JQuery framework in your extension, you add the following code to your view.html.php or tmpl file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
JHtml::_(&#039;jquery.framework&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using the framework method ensures JQuery is included correctly, only once, and ensures it doesn&#039;t clash with Mootools (if also loaded).&lt;br /&gt;
&lt;br /&gt;
Then it is just a matter of leveraging the JQuery framework:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
JFactory::getDocument()-&amp;gt;addScriptDeclaration(&#039;&lt;br /&gt;
(function($) {&lt;br /&gt;
    $(document).ready(function() {&lt;br /&gt;
        alert($(&amp;quot;#list li&amp;quot;).size());&lt;br /&gt;
    });&lt;br /&gt;
})(jQuery);&lt;br /&gt;
&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
More information about JQuery is available at http://docs.jquery.com/How_jQuery_Works. For API documentation, visit http://docs.jquery.com/.&lt;br /&gt;
&lt;br /&gt;
=== Mootools ===&lt;br /&gt;
&lt;br /&gt;
Unless you are maintaining Javascript code which leverages Mootools or you are developing an extension for a Joomla version prior to 3.0 it is recommended you use JQuery instead.&lt;br /&gt;
&lt;br /&gt;
Firstly, you will need to include the Mootools code in your extension. To include the Mootools framework in your extension, you add the following code to your view.html.php or tmpl file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
JHtml::_(&#039;behavior.framework&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code results in the same outcome as the similar JQuery framework statement; that is it ensures Mootools is included correctly and only once.&lt;br /&gt;
&lt;br /&gt;
Then using Mootools is almost identical to JQuery:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
JFactory::getDocument()-&amp;gt;addScriptDeclaration(&#039;&lt;br /&gt;
window.addEvent(&#039;domready&#039;, function() {&lt;br /&gt;
    alert($(&amp;quot;list&amp;quot;).getElements(&amp;quot;li&amp;quot;).length);&lt;br /&gt;
});&lt;br /&gt;
&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
More information about Mootools is available at http://mootools.net/. For API documentation, visit http://mootools.net/docs/core.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
&lt;br /&gt;
[[JHtmlBehavior::framework/11.1| JHtmlBehaviour::framework method]] from the Joomla! Framework 11.1 documentation&lt;br /&gt;
&lt;br /&gt;
[[Ajax using MooTools]]&lt;br /&gt;
&lt;br /&gt;
[[Adding Javascript moo.fx to your component]]&lt;br /&gt;
&lt;br /&gt;
== External Links ==&lt;br /&gt;
http://api.joomla.org/Joomla-Platform/HTML/JHtmlBehavior.html#methodframework&lt;br /&gt;
&lt;br /&gt;
http://en.wikipedia.org/wiki/JavaScript&lt;br /&gt;
&lt;br /&gt;
http://www.w3schools.com/js/default.asp&lt;br /&gt;
&lt;br /&gt;
http://mootools.net/&lt;br /&gt;
&lt;br /&gt;
http://jquery.com/&lt;br /&gt;
&lt;br /&gt;
[[Category:Templates]]&lt;br /&gt;
[[Category:Modules]]&lt;br /&gt;
[[Category:Components]]&lt;br /&gt;
[[Category:Plugins]]&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>Haydenyoung</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Adding_JavaScript&amp;diff=76402</id>
		<title>Adding JavaScript</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Adding_JavaScript&amp;diff=76402"/>
		<updated>2012-10-11T08:24:18Z</updated>

		<summary type="html">&lt;p&gt;Haydenyoung: /* External Links */ Link to latest api docs.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;JavaScript (also known as ECMAScript) is a scripting language primarily used in client-side web site development to extend and enhance an end-user&#039;s experience. Joomla provides developers with easy-to-use mechanisms to include JavaScript in their extensions using existing API methods.&lt;br /&gt;
&lt;br /&gt;
There are a number of methods for including JavaScript in your Joomla extensions; some of these are described below.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
&lt;br /&gt;
There are three methods for embedding JavaScript into your code using the Joomla API; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration], [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] and [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script]. These methods should be called either in your component&#039;s View class (&amp;lt;yourcomponent&amp;gt;/views/&amp;lt;yourview&amp;gt;/view.html.php) or template script (&amp;lt;yourcomponent&amp;gt;/views/&amp;lt;yourview&amp;gt;/tmpl/&amp;lt;yourtemplate&amp;gt;.php or in the case of a module, in its template script (&amp;lt;yourmodule&amp;gt;/tmpl/&amp;lt;yourtemplate&amp;gt;.php). &lt;br /&gt;
&lt;br /&gt;
===Inline JavaScript===&lt;br /&gt;
&lt;br /&gt;
Blocks of JavaScript code can be declared directly within a component or module&#039;s display template using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html JDocument] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration addScriptDeclaration] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$document = &amp;amp;JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addScriptDeclaration(&#039;&lt;br /&gt;
    window.event(&amp;quot;domready&amp;quot;, function() {&lt;br /&gt;
        alert(&amp;quot;An inline JavaScript Declaration&amp;quot;);&lt;br /&gt;
    });&lt;br /&gt;
&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===External JavaScript===&lt;br /&gt;
&lt;br /&gt;
Alternatively, you may wish to separate your JavaScript into a separate file. Separating your JavaScript into an external file can make your template code easier to read especially if the JavaScript is lengthy or complex.&lt;br /&gt;
&lt;br /&gt;
There are two ways to include a JavaScript file using the Joomla API. The first involves using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html JDocument] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript addScript] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$document = &amp;amp;JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addScript(&#039;/media/system/js/sample.js&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The second uses the [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html JHTML] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// Add the path parameter if the path is different than &#039;media/system/js/&#039;&lt;br /&gt;
JHTML::script(&#039;sample.js&#039;, &#039;templates/custom/js/&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
The Joomla API&#039;s [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration], [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] and [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] methods embed JavaScript into Joomla&#039;s index.php via the jdoc head tag:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;jdoc:include type=&amp;quot;head&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] or [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] methods to embed JavaScript includes would result in the index.php rendering the following HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;script type=&amp;quot;text/javaScript&amp;quot; src=&amp;quot;/media/system/js/sample.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Calling the class method [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration] would render the following HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;script type=&amp;quot;text/javaScript&amp;quot;&amp;gt;&lt;br /&gt;
window.addEvent(&amp;quot;domready&amp;quot;, function() {&lt;br /&gt;
    alert(&amp;quot;Embedded block of JS here&amp;quot;);&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/script&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using these methods is highly recommended as it clearly differentiates another scripting language (JavaScript) from the main PHP code, ensures all JavaScript is correctly embedded between the &amp;lt;head&amp;gt;&amp;lt;/head&amp;gt; tags and, in the case of JDocument::addScript and JHTML::script ensures that a JavaScript file is included only once (I.e. there is no .js file duplication).&lt;br /&gt;
&lt;br /&gt;
== Using a JavaScript Framework ==&lt;br /&gt;
&lt;br /&gt;
A Javascript framework provides developers with generic functionality for handling various coding tasks in a familiar, consistent and platform-independent way. A framework enables the developer to forget about the intricacies of implementing a certain function in different web browsers and focus on the requirements of the software.&lt;br /&gt;
&lt;br /&gt;
Two Javascript frameworks are provided as part of Joomla 3.0; JQuery and Mootools. JQuery is a newly introduced framework which integrates with Joomla&#039;s new Bootstrap HTML framework; Mootools is Joomla&#039;s legacy Javascript library which is now superseded by JQuery and is included for backwards compatibility with 3rd party extensions.&lt;br /&gt;
&lt;br /&gt;
In nearly all cases you should use a framework when developing Javascript in your extensions or templates and including one is very simple with Joomla&#039;s API.&lt;br /&gt;
&lt;br /&gt;
=== JQuery ===&lt;br /&gt;
&lt;br /&gt;
As of Joomla 3.0 it is highly recommended you use the JQuery framework for all your Javascript as it is loaded by nearly every template which uses the Bootstrap HTML/CSS framework. In those few instances where JQuery is not available, you can easily include the framework using the JHTMLJQuery class&#039; framework method.&lt;br /&gt;
&lt;br /&gt;
To include the JQuery framework in your extension, you add the following code to your view.html.php or tmpl file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
JHtml::_(&#039;jquery.framework&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using the framework method ensures JQuery is included correctly, only once, and ensures it doesn&#039;t clash with Mootools (if also loaded).&lt;br /&gt;
&lt;br /&gt;
Then it is just a matter of leveraging the JQuery framework:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
JFactory::getDocument()-&amp;gt;addScriptDeclaration(&#039;&lt;br /&gt;
(function($) {&lt;br /&gt;
    $(document).ready(function() {&lt;br /&gt;
        alert($(&amp;quot;#list li&amp;quot;).size());&lt;br /&gt;
    });&lt;br /&gt;
})(jQuery);&lt;br /&gt;
&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
More information about JQuery is available at http://docs.jquery.com/How_jQuery_Works. For API documentation, visit http://docs.jquery.com/.&lt;br /&gt;
&lt;br /&gt;
=== Mootools ===&lt;br /&gt;
&lt;br /&gt;
Unless you are maintaining Javascript code which leverages Mootools or you are developing an extension for a Joomla version prior to 3.0 it is recommended you use JQuery instead.&lt;br /&gt;
&lt;br /&gt;
Firstly, you will need to include the Mootools code in your extension. To include the Mootools framework in your extension, you add the following code to your view.html.php or tmpl file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
JHtml::_(&#039;behavior.framework&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code results in the same outcome as the similar JQuery framework statement; that is it ensures Mootools is included correctly and only once.&lt;br /&gt;
&lt;br /&gt;
Then using Mootools is almost identical to JQuery:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
JFactory::getDocument()-&amp;gt;addScriptDeclaration(&#039;&lt;br /&gt;
window.addEvent(&#039;domready&#039;, function() {&lt;br /&gt;
    alert($(&amp;quot;list&amp;quot;).getElements(&amp;quot;li&amp;quot;).length);&lt;br /&gt;
});&lt;br /&gt;
&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
More information about Mootools is available at http://mootools.net/. For API documentation, visit http://mootools.net/docs/core.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
&lt;br /&gt;
[[JHtmlBehavior::framework/11.1| JHtmlBehaviour::framework method]] from the Joomla! Framework 11.1 documentation&lt;br /&gt;
&lt;br /&gt;
[[Ajax using MooTools]]&lt;br /&gt;
&lt;br /&gt;
[[Adding Javascript moo.fx to your component]]&lt;br /&gt;
&lt;br /&gt;
== External Links ==&lt;br /&gt;
http://api.joomla.org/Joomla-Platform/HTML/JHtmlBehavior.html#methodframework&lt;br /&gt;
&lt;br /&gt;
http://en.wikipedia.org/wiki/JavaScript&lt;br /&gt;
&lt;br /&gt;
http://www.w3schools.com/js/default.asp&lt;br /&gt;
&lt;br /&gt;
http://mootools.net/&lt;br /&gt;
&lt;br /&gt;
http://jquery.com/&lt;br /&gt;
&lt;br /&gt;
[[Category:Templates]]&lt;br /&gt;
[[Category:Modules]]&lt;br /&gt;
[[Category:Components]]&lt;br /&gt;
[[Category:Plugins]]&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>Haydenyoung</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Adding_JavaScript&amp;diff=76401</id>
		<title>Adding JavaScript</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Adding_JavaScript&amp;diff=76401"/>
		<updated>2012-10-11T08:23:42Z</updated>

		<summary type="html">&lt;p&gt;Haydenyoung: /* See Also */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;JavaScript (also known as ECMAScript) is a scripting language primarily used in client-side web site development to extend and enhance an end-user&#039;s experience. Joomla provides developers with easy-to-use mechanisms to include JavaScript in their extensions using existing API methods.&lt;br /&gt;
&lt;br /&gt;
There are a number of methods for including JavaScript in your Joomla extensions; some of these are described below.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
&lt;br /&gt;
There are three methods for embedding JavaScript into your code using the Joomla API; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration], [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] and [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script]. These methods should be called either in your component&#039;s View class (&amp;lt;yourcomponent&amp;gt;/views/&amp;lt;yourview&amp;gt;/view.html.php) or template script (&amp;lt;yourcomponent&amp;gt;/views/&amp;lt;yourview&amp;gt;/tmpl/&amp;lt;yourtemplate&amp;gt;.php or in the case of a module, in its template script (&amp;lt;yourmodule&amp;gt;/tmpl/&amp;lt;yourtemplate&amp;gt;.php). &lt;br /&gt;
&lt;br /&gt;
===Inline JavaScript===&lt;br /&gt;
&lt;br /&gt;
Blocks of JavaScript code can be declared directly within a component or module&#039;s display template using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html JDocument] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration addScriptDeclaration] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$document = &amp;amp;JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addScriptDeclaration(&#039;&lt;br /&gt;
    window.event(&amp;quot;domready&amp;quot;, function() {&lt;br /&gt;
        alert(&amp;quot;An inline JavaScript Declaration&amp;quot;);&lt;br /&gt;
    });&lt;br /&gt;
&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===External JavaScript===&lt;br /&gt;
&lt;br /&gt;
Alternatively, you may wish to separate your JavaScript into a separate file. Separating your JavaScript into an external file can make your template code easier to read especially if the JavaScript is lengthy or complex.&lt;br /&gt;
&lt;br /&gt;
There are two ways to include a JavaScript file using the Joomla API. The first involves using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html JDocument] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript addScript] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$document = &amp;amp;JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addScript(&#039;/media/system/js/sample.js&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The second uses the [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html JHTML] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// Add the path parameter if the path is different than &#039;media/system/js/&#039;&lt;br /&gt;
JHTML::script(&#039;sample.js&#039;, &#039;templates/custom/js/&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
The Joomla API&#039;s [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration], [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] and [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] methods embed JavaScript into Joomla&#039;s index.php via the jdoc head tag:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;jdoc:include type=&amp;quot;head&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] or [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] methods to embed JavaScript includes would result in the index.php rendering the following HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;script type=&amp;quot;text/javaScript&amp;quot; src=&amp;quot;/media/system/js/sample.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Calling the class method [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration] would render the following HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;script type=&amp;quot;text/javaScript&amp;quot;&amp;gt;&lt;br /&gt;
window.addEvent(&amp;quot;domready&amp;quot;, function() {&lt;br /&gt;
    alert(&amp;quot;Embedded block of JS here&amp;quot;);&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/script&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using these methods is highly recommended as it clearly differentiates another scripting language (JavaScript) from the main PHP code, ensures all JavaScript is correctly embedded between the &amp;lt;head&amp;gt;&amp;lt;/head&amp;gt; tags and, in the case of JDocument::addScript and JHTML::script ensures that a JavaScript file is included only once (I.e. there is no .js file duplication).&lt;br /&gt;
&lt;br /&gt;
== Using a JavaScript Framework ==&lt;br /&gt;
&lt;br /&gt;
A Javascript framework provides developers with generic functionality for handling various coding tasks in a familiar, consistent and platform-independent way. A framework enables the developer to forget about the intricacies of implementing a certain function in different web browsers and focus on the requirements of the software.&lt;br /&gt;
&lt;br /&gt;
Two Javascript frameworks are provided as part of Joomla 3.0; JQuery and Mootools. JQuery is a newly introduced framework which integrates with Joomla&#039;s new Bootstrap HTML framework; Mootools is Joomla&#039;s legacy Javascript library which is now superseded by JQuery and is included for backwards compatibility with 3rd party extensions.&lt;br /&gt;
&lt;br /&gt;
In nearly all cases you should use a framework when developing Javascript in your extensions or templates and including one is very simple with Joomla&#039;s API.&lt;br /&gt;
&lt;br /&gt;
=== JQuery ===&lt;br /&gt;
&lt;br /&gt;
As of Joomla 3.0 it is highly recommended you use the JQuery framework for all your Javascript as it is loaded by nearly every template which uses the Bootstrap HTML/CSS framework. In those few instances where JQuery is not available, you can easily include the framework using the JHTMLJQuery class&#039; framework method.&lt;br /&gt;
&lt;br /&gt;
To include the JQuery framework in your extension, you add the following code to your view.html.php or tmpl file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
JHtml::_(&#039;jquery.framework&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using the framework method ensures JQuery is included correctly, only once, and ensures it doesn&#039;t clash with Mootools (if also loaded).&lt;br /&gt;
&lt;br /&gt;
Then it is just a matter of leveraging the JQuery framework:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
JFactory::getDocument()-&amp;gt;addScriptDeclaration(&#039;&lt;br /&gt;
(function($) {&lt;br /&gt;
    $(document).ready(function() {&lt;br /&gt;
        alert($(&amp;quot;#list li&amp;quot;).size());&lt;br /&gt;
    });&lt;br /&gt;
})(jQuery);&lt;br /&gt;
&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
More information about JQuery is available at http://docs.jquery.com/How_jQuery_Works. For API documentation, visit http://docs.jquery.com/.&lt;br /&gt;
&lt;br /&gt;
=== Mootools ===&lt;br /&gt;
&lt;br /&gt;
Unless you are maintaining Javascript code which leverages Mootools or you are developing an extension for a Joomla version prior to 3.0 it is recommended you use JQuery instead.&lt;br /&gt;
&lt;br /&gt;
Firstly, you will need to include the Mootools code in your extension. To include the Mootools framework in your extension, you add the following code to your view.html.php or tmpl file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
JHtml::_(&#039;behavior.framework&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code results in the same outcome as the similar JQuery framework statement; that is it ensures Mootools is included correctly and only once.&lt;br /&gt;
&lt;br /&gt;
Then using Mootools is almost identical to JQuery:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
JFactory::getDocument()-&amp;gt;addScriptDeclaration(&#039;&lt;br /&gt;
window.addEvent(&#039;domready&#039;, function() {&lt;br /&gt;
    alert($(&amp;quot;list&amp;quot;).getElements(&amp;quot;li&amp;quot;).length);&lt;br /&gt;
});&lt;br /&gt;
&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
More information about Mootools is available at http://mootools.net/. For API documentation, visit http://mootools.net/docs/core.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
&lt;br /&gt;
[[JHtmlBehavior::framework/11.1| JHtmlBehaviour::framework method]] from the Joomla! Framework 11.1 documentation&lt;br /&gt;
&lt;br /&gt;
[[Ajax using MooTools]]&lt;br /&gt;
&lt;br /&gt;
[[Adding Javascript moo.fx to your component]]&lt;br /&gt;
&lt;br /&gt;
== External Links ==&lt;br /&gt;
&lt;br /&gt;
http://en.wikipedia.org/wiki/JavaScript&lt;br /&gt;
&lt;br /&gt;
http://www.w3schools.com/js/default.asp&lt;br /&gt;
&lt;br /&gt;
http://mootools.net/&lt;br /&gt;
&lt;br /&gt;
http://jquery.com/&lt;br /&gt;
&lt;br /&gt;
[[Category:Templates]]&lt;br /&gt;
[[Category:Modules]]&lt;br /&gt;
[[Category:Components]]&lt;br /&gt;
[[Category:Plugins]]&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>Haydenyoung</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Adding_JavaScript&amp;diff=76400</id>
		<title>Adding JavaScript</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Adding_JavaScript&amp;diff=76400"/>
		<updated>2012-10-11T08:21:32Z</updated>

		<summary type="html">&lt;p&gt;Haydenyoung: /* JQuery */ Links to getting started with JQuery.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;JavaScript (also known as ECMAScript) is a scripting language primarily used in client-side web site development to extend and enhance an end-user&#039;s experience. Joomla provides developers with easy-to-use mechanisms to include JavaScript in their extensions using existing API methods.&lt;br /&gt;
&lt;br /&gt;
There are a number of methods for including JavaScript in your Joomla extensions; some of these are described below.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
&lt;br /&gt;
There are three methods for embedding JavaScript into your code using the Joomla API; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration], [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] and [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script]. These methods should be called either in your component&#039;s View class (&amp;lt;yourcomponent&amp;gt;/views/&amp;lt;yourview&amp;gt;/view.html.php) or template script (&amp;lt;yourcomponent&amp;gt;/views/&amp;lt;yourview&amp;gt;/tmpl/&amp;lt;yourtemplate&amp;gt;.php or in the case of a module, in its template script (&amp;lt;yourmodule&amp;gt;/tmpl/&amp;lt;yourtemplate&amp;gt;.php). &lt;br /&gt;
&lt;br /&gt;
===Inline JavaScript===&lt;br /&gt;
&lt;br /&gt;
Blocks of JavaScript code can be declared directly within a component or module&#039;s display template using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html JDocument] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration addScriptDeclaration] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$document = &amp;amp;JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addScriptDeclaration(&#039;&lt;br /&gt;
    window.event(&amp;quot;domready&amp;quot;, function() {&lt;br /&gt;
        alert(&amp;quot;An inline JavaScript Declaration&amp;quot;);&lt;br /&gt;
    });&lt;br /&gt;
&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===External JavaScript===&lt;br /&gt;
&lt;br /&gt;
Alternatively, you may wish to separate your JavaScript into a separate file. Separating your JavaScript into an external file can make your template code easier to read especially if the JavaScript is lengthy or complex.&lt;br /&gt;
&lt;br /&gt;
There are two ways to include a JavaScript file using the Joomla API. The first involves using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html JDocument] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript addScript] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$document = &amp;amp;JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addScript(&#039;/media/system/js/sample.js&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The second uses the [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html JHTML] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// Add the path parameter if the path is different than &#039;media/system/js/&#039;&lt;br /&gt;
JHTML::script(&#039;sample.js&#039;, &#039;templates/custom/js/&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
The Joomla API&#039;s [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration], [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] and [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] methods embed JavaScript into Joomla&#039;s index.php via the jdoc head tag:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;jdoc:include type=&amp;quot;head&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] or [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] methods to embed JavaScript includes would result in the index.php rendering the following HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;script type=&amp;quot;text/javaScript&amp;quot; src=&amp;quot;/media/system/js/sample.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Calling the class method [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration] would render the following HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;script type=&amp;quot;text/javaScript&amp;quot;&amp;gt;&lt;br /&gt;
window.addEvent(&amp;quot;domready&amp;quot;, function() {&lt;br /&gt;
    alert(&amp;quot;Embedded block of JS here&amp;quot;);&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/script&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using these methods is highly recommended as it clearly differentiates another scripting language (JavaScript) from the main PHP code, ensures all JavaScript is correctly embedded between the &amp;lt;head&amp;gt;&amp;lt;/head&amp;gt; tags and, in the case of JDocument::addScript and JHTML::script ensures that a JavaScript file is included only once (I.e. there is no .js file duplication).&lt;br /&gt;
&lt;br /&gt;
== Using a JavaScript Framework ==&lt;br /&gt;
&lt;br /&gt;
A Javascript framework provides developers with generic functionality for handling various coding tasks in a familiar, consistent and platform-independent way. A framework enables the developer to forget about the intricacies of implementing a certain function in different web browsers and focus on the requirements of the software.&lt;br /&gt;
&lt;br /&gt;
Two Javascript frameworks are provided as part of Joomla 3.0; JQuery and Mootools. JQuery is a newly introduced framework which integrates with Joomla&#039;s new Bootstrap HTML framework; Mootools is Joomla&#039;s legacy Javascript library which is now superseded by JQuery and is included for backwards compatibility with 3rd party extensions.&lt;br /&gt;
&lt;br /&gt;
In nearly all cases you should use a framework when developing Javascript in your extensions or templates and including one is very simple with Joomla&#039;s API.&lt;br /&gt;
&lt;br /&gt;
=== JQuery ===&lt;br /&gt;
&lt;br /&gt;
As of Joomla 3.0 it is highly recommended you use the JQuery framework for all your Javascript as it is loaded by nearly every template which uses the Bootstrap HTML/CSS framework. In those few instances where JQuery is not available, you can easily include the framework using the JHTMLJQuery class&#039; framework method.&lt;br /&gt;
&lt;br /&gt;
To include the JQuery framework in your extension, you add the following code to your view.html.php or tmpl file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
JHtml::_(&#039;jquery.framework&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using the framework method ensures JQuery is included correctly, only once, and ensures it doesn&#039;t clash with Mootools (if also loaded).&lt;br /&gt;
&lt;br /&gt;
Then it is just a matter of leveraging the JQuery framework:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
JFactory::getDocument()-&amp;gt;addScriptDeclaration(&#039;&lt;br /&gt;
(function($) {&lt;br /&gt;
    $(document).ready(function() {&lt;br /&gt;
        alert($(&amp;quot;#list li&amp;quot;).size());&lt;br /&gt;
    });&lt;br /&gt;
})(jQuery);&lt;br /&gt;
&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
More information about JQuery is available at http://docs.jquery.com/How_jQuery_Works. For API documentation, visit http://docs.jquery.com/.&lt;br /&gt;
&lt;br /&gt;
=== Mootools ===&lt;br /&gt;
&lt;br /&gt;
Unless you are maintaining Javascript code which leverages Mootools or you are developing an extension for a Joomla version prior to 3.0 it is recommended you use JQuery instead.&lt;br /&gt;
&lt;br /&gt;
Firstly, you will need to include the Mootools code in your extension. To include the Mootools framework in your extension, you add the following code to your view.html.php or tmpl file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
JHtml::_(&#039;behavior.framework&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code results in the same outcome as the similar JQuery framework statement; that is it ensures Mootools is included correctly and only once.&lt;br /&gt;
&lt;br /&gt;
Then using Mootools is almost identical to JQuery:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
JFactory::getDocument()-&amp;gt;addScriptDeclaration(&#039;&lt;br /&gt;
window.addEvent(&#039;domready&#039;, function() {&lt;br /&gt;
    alert($(&amp;quot;list&amp;quot;).getElements(&amp;quot;li&amp;quot;).length);&lt;br /&gt;
});&lt;br /&gt;
&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
More information about Mootools is available at http://mootools.net/. For API documentation, visit http://mootools.net/docs/core.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
&lt;br /&gt;
[[JHtmlBehavior::framework/11.1| JHtmlBehaviour::framework method]]&lt;br /&gt;
&lt;br /&gt;
[[Ajax using MooTools]]&lt;br /&gt;
&lt;br /&gt;
[[Adding Javascript moo.fx to your component]]&lt;br /&gt;
&lt;br /&gt;
== External Links ==&lt;br /&gt;
&lt;br /&gt;
http://en.wikipedia.org/wiki/JavaScript&lt;br /&gt;
&lt;br /&gt;
http://www.w3schools.com/js/default.asp&lt;br /&gt;
&lt;br /&gt;
http://mootools.net/&lt;br /&gt;
&lt;br /&gt;
http://jquery.com/&lt;br /&gt;
&lt;br /&gt;
[[Category:Templates]]&lt;br /&gt;
[[Category:Modules]]&lt;br /&gt;
[[Category:Components]]&lt;br /&gt;
[[Category:Plugins]]&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>Haydenyoung</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Adding_JavaScript&amp;diff=76399</id>
		<title>Adding JavaScript</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Adding_JavaScript&amp;diff=76399"/>
		<updated>2012-10-11T08:19:08Z</updated>

		<summary type="html">&lt;p&gt;Haydenyoung: /* Mootools */  Some links to mootools.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;JavaScript (also known as ECMAScript) is a scripting language primarily used in client-side web site development to extend and enhance an end-user&#039;s experience. Joomla provides developers with easy-to-use mechanisms to include JavaScript in their extensions using existing API methods.&lt;br /&gt;
&lt;br /&gt;
There are a number of methods for including JavaScript in your Joomla extensions; some of these are described below.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
&lt;br /&gt;
There are three methods for embedding JavaScript into your code using the Joomla API; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration], [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] and [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script]. These methods should be called either in your component&#039;s View class (&amp;lt;yourcomponent&amp;gt;/views/&amp;lt;yourview&amp;gt;/view.html.php) or template script (&amp;lt;yourcomponent&amp;gt;/views/&amp;lt;yourview&amp;gt;/tmpl/&amp;lt;yourtemplate&amp;gt;.php or in the case of a module, in its template script (&amp;lt;yourmodule&amp;gt;/tmpl/&amp;lt;yourtemplate&amp;gt;.php). &lt;br /&gt;
&lt;br /&gt;
===Inline JavaScript===&lt;br /&gt;
&lt;br /&gt;
Blocks of JavaScript code can be declared directly within a component or module&#039;s display template using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html JDocument] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration addScriptDeclaration] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$document = &amp;amp;JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addScriptDeclaration(&#039;&lt;br /&gt;
    window.event(&amp;quot;domready&amp;quot;, function() {&lt;br /&gt;
        alert(&amp;quot;An inline JavaScript Declaration&amp;quot;);&lt;br /&gt;
    });&lt;br /&gt;
&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===External JavaScript===&lt;br /&gt;
&lt;br /&gt;
Alternatively, you may wish to separate your JavaScript into a separate file. Separating your JavaScript into an external file can make your template code easier to read especially if the JavaScript is lengthy or complex.&lt;br /&gt;
&lt;br /&gt;
There are two ways to include a JavaScript file using the Joomla API. The first involves using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html JDocument] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript addScript] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$document = &amp;amp;JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addScript(&#039;/media/system/js/sample.js&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The second uses the [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html JHTML] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// Add the path parameter if the path is different than &#039;media/system/js/&#039;&lt;br /&gt;
JHTML::script(&#039;sample.js&#039;, &#039;templates/custom/js/&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
The Joomla API&#039;s [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration], [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] and [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] methods embed JavaScript into Joomla&#039;s index.php via the jdoc head tag:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;jdoc:include type=&amp;quot;head&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] or [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] methods to embed JavaScript includes would result in the index.php rendering the following HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;script type=&amp;quot;text/javaScript&amp;quot; src=&amp;quot;/media/system/js/sample.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Calling the class method [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration] would render the following HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;script type=&amp;quot;text/javaScript&amp;quot;&amp;gt;&lt;br /&gt;
window.addEvent(&amp;quot;domready&amp;quot;, function() {&lt;br /&gt;
    alert(&amp;quot;Embedded block of JS here&amp;quot;);&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/script&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using these methods is highly recommended as it clearly differentiates another scripting language (JavaScript) from the main PHP code, ensures all JavaScript is correctly embedded between the &amp;lt;head&amp;gt;&amp;lt;/head&amp;gt; tags and, in the case of JDocument::addScript and JHTML::script ensures that a JavaScript file is included only once (I.e. there is no .js file duplication).&lt;br /&gt;
&lt;br /&gt;
== Using a JavaScript Framework ==&lt;br /&gt;
&lt;br /&gt;
A Javascript framework provides developers with generic functionality for handling various coding tasks in a familiar, consistent and platform-independent way. A framework enables the developer to forget about the intricacies of implementing a certain function in different web browsers and focus on the requirements of the software.&lt;br /&gt;
&lt;br /&gt;
Two Javascript frameworks are provided as part of Joomla 3.0; JQuery and Mootools. JQuery is a newly introduced framework which integrates with Joomla&#039;s new Bootstrap HTML framework; Mootools is Joomla&#039;s legacy Javascript library which is now superseded by JQuery and is included for backwards compatibility with 3rd party extensions.&lt;br /&gt;
&lt;br /&gt;
In nearly all cases you should use a framework when developing Javascript in your extensions or templates and including one is very simple with Joomla&#039;s API.&lt;br /&gt;
&lt;br /&gt;
=== JQuery ===&lt;br /&gt;
&lt;br /&gt;
As of Joomla 3.0 it is highly recommended you use the JQuery framework for all your Javascript as it is loaded by nearly every template which uses the Bootstrap HTML/CSS framework. In those few instances where JQuery is not available, you can easily include the framework using the JHTMLJQuery class&#039; framework method.&lt;br /&gt;
&lt;br /&gt;
To include the JQuery framework in your extension, you add the following code to your view.html.php or tmpl file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
JHtml::_(&#039;jquery.framework&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using the framework method ensures JQuery is included correctly, only once, and ensures it doesn&#039;t clash with Mootools (if also loaded).&lt;br /&gt;
&lt;br /&gt;
Then it is just a matter of leveraging the JQuery framework:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
JFactory::getDocument()-&amp;gt;addScriptDeclaration(&#039;&lt;br /&gt;
(function($) {&lt;br /&gt;
    $(document).ready(function() {&lt;br /&gt;
        alert($(&amp;quot;#list li&amp;quot;).size());&lt;br /&gt;
    });&lt;br /&gt;
})(jQuery);&lt;br /&gt;
&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Mootools ===&lt;br /&gt;
&lt;br /&gt;
Unless you are maintaining Javascript code which leverages Mootools or you are developing an extension for a Joomla version prior to 3.0 it is recommended you use JQuery instead.&lt;br /&gt;
&lt;br /&gt;
Firstly, you will need to include the Mootools code in your extension. To include the Mootools framework in your extension, you add the following code to your view.html.php or tmpl file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
JHtml::_(&#039;behavior.framework&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code results in the same outcome as the similar JQuery framework statement; that is it ensures Mootools is included correctly and only once.&lt;br /&gt;
&lt;br /&gt;
Then using Mootools is almost identical to JQuery:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
JFactory::getDocument()-&amp;gt;addScriptDeclaration(&#039;&lt;br /&gt;
window.addEvent(&#039;domready&#039;, function() {&lt;br /&gt;
    alert($(&amp;quot;list&amp;quot;).getElements(&amp;quot;li&amp;quot;).length);&lt;br /&gt;
});&lt;br /&gt;
&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
More information about Mootools is available at http://mootools.net/. For API documentation, visit http://mootools.net/docs/core.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
&lt;br /&gt;
[[JHtmlBehavior::framework/11.1| JHtmlBehaviour::framework method]]&lt;br /&gt;
&lt;br /&gt;
[[Ajax using MooTools]]&lt;br /&gt;
&lt;br /&gt;
[[Adding Javascript moo.fx to your component]]&lt;br /&gt;
&lt;br /&gt;
== External Links ==&lt;br /&gt;
&lt;br /&gt;
http://en.wikipedia.org/wiki/JavaScript&lt;br /&gt;
&lt;br /&gt;
http://www.w3schools.com/js/default.asp&lt;br /&gt;
&lt;br /&gt;
http://mootools.net/&lt;br /&gt;
&lt;br /&gt;
http://jquery.com/&lt;br /&gt;
&lt;br /&gt;
[[Category:Templates]]&lt;br /&gt;
[[Category:Modules]]&lt;br /&gt;
[[Category:Components]]&lt;br /&gt;
[[Category:Plugins]]&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>Haydenyoung</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Adding_JavaScript&amp;diff=76239</id>
		<title>Adding JavaScript</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Adding_JavaScript&amp;diff=76239"/>
		<updated>2012-10-08T06:42:40Z</updated>

		<summary type="html">&lt;p&gt;Haydenyoung: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;JavaScript (also known as ECMAScript) is a scripting language primarily used in client-side web site development to extend and enhance an end-user&#039;s experience. Joomla provides developers with easy-to-use mechanisms to include JavaScript in their extensions using existing API methods.&lt;br /&gt;
&lt;br /&gt;
There are a number of methods for including JavaScript in your Joomla extensions; some of these are described below.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
&lt;br /&gt;
There are three methods for embedding JavaScript into your code using the Joomla API; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration], [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] and [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script]. These methods should be called either in your component&#039;s View class (&amp;lt;yourcomponent&amp;gt;/views/&amp;lt;yourview&amp;gt;/view.html.php) or template script (&amp;lt;yourcomponent&amp;gt;/views/&amp;lt;yourview&amp;gt;/tmpl/&amp;lt;yourtemplate&amp;gt;.php or in the case of a module, in its template script (&amp;lt;yourmodule&amp;gt;/tmpl/&amp;lt;yourtemplate&amp;gt;.php). &lt;br /&gt;
&lt;br /&gt;
===Inline JavaScript===&lt;br /&gt;
&lt;br /&gt;
Blocks of JavaScript code can be declared directly within a component or module&#039;s display template using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html JDocument] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration addScriptDeclaration] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$document = &amp;amp;JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addScriptDeclaration(&#039;&lt;br /&gt;
    window.event(&amp;quot;domready&amp;quot;, function() {&lt;br /&gt;
        alert(&amp;quot;An inline JavaScript Declaration&amp;quot;);&lt;br /&gt;
    });&lt;br /&gt;
&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===External JavaScript===&lt;br /&gt;
&lt;br /&gt;
Alternatively, you may wish to separate your JavaScript into a separate file. Separating your JavaScript into an external file can make your template code easier to read especially if the JavaScript is lengthy or complex.&lt;br /&gt;
&lt;br /&gt;
There are two ways to include a JavaScript file using the Joomla API. The first involves using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html JDocument] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript addScript] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$document = &amp;amp;JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addScript(&#039;/media/system/js/sample.js&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The second uses the [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html JHTML] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// Add the path parameter if the path is different than &#039;media/system/js/&#039;&lt;br /&gt;
JHTML::script(&#039;sample.js&#039;, &#039;templates/custom/js/&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
The Joomla API&#039;s [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration], [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] and [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] methods embed JavaScript into Joomla&#039;s index.php via the jdoc head tag:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;jdoc:include type=&amp;quot;head&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] or [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] methods to embed JavaScript includes would result in the index.php rendering the following HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;script type=&amp;quot;text/javaScript&amp;quot; src=&amp;quot;/media/system/js/sample.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Calling the class method [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration] would render the following HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;script type=&amp;quot;text/javaScript&amp;quot;&amp;gt;&lt;br /&gt;
window.addEvent(&amp;quot;domready&amp;quot;, function() {&lt;br /&gt;
    alert(&amp;quot;Embedded block of JS here&amp;quot;);&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/script&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using these methods is highly recommended as it clearly differentiates another scripting language (JavaScript) from the main PHP code, ensures all JavaScript is correctly embedded between the &amp;lt;head&amp;gt;&amp;lt;/head&amp;gt; tags and, in the case of JDocument::addScript and JHTML::script ensures that a JavaScript file is included only once (I.e. there is no .js file duplication).&lt;br /&gt;
&lt;br /&gt;
== Using a JavaScript Framework ==&lt;br /&gt;
&lt;br /&gt;
A Javascript framework provides developers with generic functionality for handling various coding tasks in a familiar, consistent and platform-independent way. A framework enables the developer to forget about the intricacies of implementing a certain function in different web browsers and focus on the requirements of the software.&lt;br /&gt;
&lt;br /&gt;
Two Javascript frameworks are provided as part of Joomla 3.0; JQuery and Mootools. JQuery is a newly introduced framework which integrates with Joomla&#039;s new Bootstrap HTML framework; Mootools is Joomla&#039;s legacy Javascript library which is now superseded by JQuery and is included for backwards compatibility with 3rd party extensions.&lt;br /&gt;
&lt;br /&gt;
In nearly all cases you should use a framework when developing Javascript in your extensions or templates and including one is very simple with Joomla&#039;s API.&lt;br /&gt;
&lt;br /&gt;
=== JQuery ===&lt;br /&gt;
&lt;br /&gt;
As of Joomla 3.0 it is highly recommended you use the JQuery framework for all your Javascript as it is loaded by nearly every template which uses the Bootstrap HTML/CSS framework. In those few instances where JQuery is not available, you can easily include the framework using the JHTMLJQuery class&#039; framework method.&lt;br /&gt;
&lt;br /&gt;
To include the JQuery framework in your extension, you add the following code to your view.html.php or tmpl file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
JHtml::_(&#039;jquery.framework&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using the framework method ensures JQuery is included correctly, only once, and ensures it doesn&#039;t clash with Mootools (if also loaded).&lt;br /&gt;
&lt;br /&gt;
Then it is just a matter of leveraging the JQuery framework:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
JFactory::getDocument()-&amp;gt;addScriptDeclaration(&#039;&lt;br /&gt;
(function($) {&lt;br /&gt;
    $(document).ready(function() {&lt;br /&gt;
        alert($(&amp;quot;#list li&amp;quot;).size());&lt;br /&gt;
    });&lt;br /&gt;
})(jQuery);&lt;br /&gt;
&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Mootools ===&lt;br /&gt;
&lt;br /&gt;
Unless you are maintaining Javascript code which leverages Mootools or you are developing an extension for a Joomla version prior to 3.0 it is recommended you use JQuery instead.&lt;br /&gt;
&lt;br /&gt;
Firstly, you will need to include the Mootools code in your extension. To include the Mootools framework in your extension, you add the following code to your view.html.php or tmpl file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
JHtml::_(&#039;behavior.framework&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code results in the same outcome as the similar JQuery framework statement; that is it ensures Mootools is included correctly and only once.&lt;br /&gt;
&lt;br /&gt;
Then using Mootools is almost identical to JQuery:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
JFactory::getDocument()-&amp;gt;addScriptDeclaration(&#039;&lt;br /&gt;
window.addEvent(&#039;domready&#039;, function() {&lt;br /&gt;
    alert($(&amp;quot;list&amp;quot;).getElements(&amp;quot;li&amp;quot;).length);&lt;br /&gt;
});&lt;br /&gt;
&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For more information, see [[#External Links]]&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
&lt;br /&gt;
[[JHtmlBehavior::framework/11.1| JHtmlBehaviour::framework method]]&lt;br /&gt;
&lt;br /&gt;
[[Ajax using MooTools]]&lt;br /&gt;
&lt;br /&gt;
[[Adding Javascript moo.fx to your component]]&lt;br /&gt;
&lt;br /&gt;
== External Links ==&lt;br /&gt;
&lt;br /&gt;
http://en.wikipedia.org/wiki/JavaScript&lt;br /&gt;
&lt;br /&gt;
http://www.w3schools.com/js/default.asp&lt;br /&gt;
&lt;br /&gt;
http://mootools.net/&lt;br /&gt;
&lt;br /&gt;
http://jquery.com/&lt;br /&gt;
&lt;br /&gt;
[[Category:Templates]]&lt;br /&gt;
[[Category:Modules]]&lt;br /&gt;
[[Category:Components]]&lt;br /&gt;
[[Category:Plugins]]&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>Haydenyoung</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Adding_JavaScript&amp;diff=76238</id>
		<title>Adding JavaScript</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Adding_JavaScript&amp;diff=76238"/>
		<updated>2012-10-08T06:21:43Z</updated>

		<summary type="html">&lt;p&gt;Haydenyoung: Remove from Cookie Jar&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;JavaScript (also known as ECMAScript) is a scripting language primarily used in client-side web site development to extend and enhance an end-user&#039;s experience. Joomla provides developers with easy-to-use mechanisms to include JavaScript in their extensions using existing API methods.&lt;br /&gt;
&lt;br /&gt;
There are a number of methods for including JavaScript in your Joomla extensions; some of these are described below.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
&lt;br /&gt;
There are three methods for embedding JavaScript into your code using the Joomla API; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration], [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] and [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script]. These methods should be called either in your component&#039;s View class (&amp;lt;yourcomponent&amp;gt;/views/&amp;lt;yourview&amp;gt;/view.html.php) or template script (&amp;lt;yourcomponent&amp;gt;/views/&amp;lt;yourview&amp;gt;/tmpl/&amp;lt;yourtemplate&amp;gt;.php or in the case of a module, in its template script (&amp;lt;yourmodule&amp;gt;/tmpl/&amp;lt;yourtemplate&amp;gt;.php). &lt;br /&gt;
&lt;br /&gt;
===Inline JavaScript===&lt;br /&gt;
&lt;br /&gt;
Blocks of JavaScript code can be declared directly within a component or module&#039;s display template using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html JDocument] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration addScriptDeclaration] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$document = &amp;amp;JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addScriptDeclaration(&#039;&lt;br /&gt;
    window.event(&amp;quot;domready&amp;quot;, function() {&lt;br /&gt;
        alert(&amp;quot;An inline JavaScript Declaration&amp;quot;);&lt;br /&gt;
    });&lt;br /&gt;
&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===External JavaScript===&lt;br /&gt;
&lt;br /&gt;
Alternatively, you may wish to separate your JavaScript into a separate file. Separating your JavaScript into an external file can make your template code easier to read especially if the JavaScript is lengthy or complex.&lt;br /&gt;
&lt;br /&gt;
There are two ways to include a JavaScript file using the Joomla API. The first involves using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html JDocument] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript addScript] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$document = &amp;amp;JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addScript(&#039;/media/system/js/sample.js&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The second uses the [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html JHTML] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// Add the path parameter if the path is different than &#039;media/system/js/&#039;&lt;br /&gt;
JHTML::script(&#039;sample.js&#039;, &#039;templates/custom/js/&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
The Joomla API&#039;s [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration], [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] and [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] methods embed JavaScript into Joomla&#039;s index.php via the jdoc head tag:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;jdoc:include type=&amp;quot;head&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] or [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] methods to embed JavaScript includes would result in the index.php rendering the following HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;script type=&amp;quot;text/javaScript&amp;quot; src=&amp;quot;/media/system/js/sample.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Calling the class method [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration] would render the following HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;script type=&amp;quot;text/javaScript&amp;quot;&amp;gt;&lt;br /&gt;
window.addEvent(&amp;quot;domready&amp;quot;, function() {&lt;br /&gt;
    alert(&amp;quot;Embedded block of JS here&amp;quot;);&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/script&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using these methods is highly recommended as it clearly differentiates another scripting language (JavaScript) from the main PHP code, ensures all JavaScript is correctly embedded between the &amp;lt;head&amp;gt;&amp;lt;/head&amp;gt; tags and, in the case of JDocument::addScript and JHTML::script ensures that a JavaScript file is included only once (I.e. there is no .js file duplication).&lt;br /&gt;
&lt;br /&gt;
== Using a JavaScript Framework ==&lt;br /&gt;
&lt;br /&gt;
A Javascript framework provides developers with generic functionality for handling various coding tasks in a familiar, consistent and platform-independent way. A framework enables the developer to forget about the intricacies of implementing a certain function in different web browsers and focus on the requirements of the software.&lt;br /&gt;
&lt;br /&gt;
Two Javascript frameworks are provided as part of Joomla 3.0; JQuery and Mootools. JQuery is a newly introduced framework which integrates with Joomla&#039;s new Bootstrap HTML framework; Mootools is Joomla&#039;s legacy Javascript library which is now superseded by JQuery and is included for backwards compatibility with 3rd party extensions.&lt;br /&gt;
&lt;br /&gt;
In nearly all cases you should use a framework when developing Javascript in your extensions or templates and including one is very simple with Joomla&#039;s API.&lt;br /&gt;
&lt;br /&gt;
=== JQuery ===&lt;br /&gt;
&lt;br /&gt;
As of Joomla 3.0 it is highly recommended you use the JQuery framework for all your Javascript as it is loaded by nearly every template which uses the Bootstrap HTML/CSS framework. In those few instances where JQuery is not available, you can easily include the framework using the JHTMLJQuery class&#039; framework method.&lt;br /&gt;
&lt;br /&gt;
To include the JQuery framework in your extension, you add the following code to your view.html.php or tmpl file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
JHtml::_(&#039;jquery.framework&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using the framework method ensures JQuery is included correctly, only once, and ensures it doesn&#039;t clash with Mootools (if also loaded).&lt;br /&gt;
&lt;br /&gt;
Then it is just a matter of leveraging the JQuery framework:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
JFactory::getDocument()-&amp;gt;addScriptDeclaration(&#039;&lt;br /&gt;
(function($) {&lt;br /&gt;
    $(document).ready(function() {&lt;br /&gt;
        alert($(&amp;quot;#list li&amp;quot;).size());&lt;br /&gt;
    });&lt;br /&gt;
})(jQuery);&lt;br /&gt;
&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Mootools ===&lt;br /&gt;
&lt;br /&gt;
Unless you are maintaining Javascript code which leverages Mootools or you are developing an extension for a Joomla version prior to 3.0 it is recommended you use JQuery instead.&lt;br /&gt;
&lt;br /&gt;
Firstly, you will need to include the Mootools code in your extension. To include the Mootools framework in your extension, you add the following code to your view.html.php or tmpl file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
JHtml::_(&#039;behavior.framework&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code results in the same outcome as the similar JQuery framework statement; that is it ensures Mootools is included correctly and only once.&lt;br /&gt;
&lt;br /&gt;
Then using Mootools is almost identical to JQuery:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
JFactory::getDocument()-&amp;gt;addScriptDeclaration(&#039;&lt;br /&gt;
window.addEvent(&#039;domready&#039;, function() {&lt;br /&gt;
    alert($(&amp;quot;list&amp;quot;).getElements(&amp;quot;li&amp;quot;).length);&lt;br /&gt;
});&lt;br /&gt;
&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For more information, see [[#Further Reading]]&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
&lt;br /&gt;
[[JHtmlBehavior::framework/11.1| JHtmlBehaviour::framework method]]&lt;br /&gt;
&lt;br /&gt;
[[Ajax using MooTools]]&lt;br /&gt;
&lt;br /&gt;
[[Adding Javascript moo.fx to your component]]&lt;br /&gt;
&lt;br /&gt;
== External Links ==&lt;br /&gt;
&lt;br /&gt;
http://en.wikipedia.org/wiki/JavaScript&lt;br /&gt;
&lt;br /&gt;
http://www.w3schools.com/js/default.asp&lt;br /&gt;
&lt;br /&gt;
http://mootools.net/&lt;br /&gt;
&lt;br /&gt;
http://jquery.com/&lt;br /&gt;
&lt;br /&gt;
[[Category:Templates]]&lt;br /&gt;
[[Category:Modules]]&lt;br /&gt;
[[Category:Components]]&lt;br /&gt;
[[Category:Plugins]]&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>Haydenyoung</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Adding_JavaScript&amp;diff=76237</id>
		<title>Adding JavaScript</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Adding_JavaScript&amp;diff=76237"/>
		<updated>2012-10-08T06:21:10Z</updated>

		<summary type="html">&lt;p&gt;Haydenyoung: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{cookiejar}}&lt;br /&gt;
&lt;br /&gt;
JavaScript (also known as ECMAScript) is a scripting language primarily used in client-side web site development to extend and enhance an end-user&#039;s experience. Joomla provides developers with easy-to-use mechanisms to include JavaScript in their extensions using existing API methods.&lt;br /&gt;
&lt;br /&gt;
There are a number of methods for including JavaScript in your Joomla extensions; some of these are described below.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
&lt;br /&gt;
There are three methods for embedding JavaScript into your code using the Joomla API; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration], [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] and [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script]. These methods should be called either in your component&#039;s View class (&amp;lt;yourcomponent&amp;gt;/views/&amp;lt;yourview&amp;gt;/view.html.php) or template script (&amp;lt;yourcomponent&amp;gt;/views/&amp;lt;yourview&amp;gt;/tmpl/&amp;lt;yourtemplate&amp;gt;.php or in the case of a module, in its template script (&amp;lt;yourmodule&amp;gt;/tmpl/&amp;lt;yourtemplate&amp;gt;.php). &lt;br /&gt;
&lt;br /&gt;
===Inline JavaScript===&lt;br /&gt;
&lt;br /&gt;
Blocks of JavaScript code can be declared directly within a component or module&#039;s display template using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html JDocument] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration addScriptDeclaration] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$document = &amp;amp;JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addScriptDeclaration(&#039;&lt;br /&gt;
    window.event(&amp;quot;domready&amp;quot;, function() {&lt;br /&gt;
        alert(&amp;quot;An inline JavaScript Declaration&amp;quot;);&lt;br /&gt;
    });&lt;br /&gt;
&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===External JavaScript===&lt;br /&gt;
&lt;br /&gt;
Alternatively, you may wish to separate your JavaScript into a separate file. Separating your JavaScript into an external file can make your template code easier to read especially if the JavaScript is lengthy or complex.&lt;br /&gt;
&lt;br /&gt;
There are two ways to include a JavaScript file using the Joomla API. The first involves using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html JDocument] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript addScript] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$document = &amp;amp;JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addScript(&#039;/media/system/js/sample.js&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The second uses the [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html JHTML] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// Add the path parameter if the path is different than &#039;media/system/js/&#039;&lt;br /&gt;
JHTML::script(&#039;sample.js&#039;, &#039;templates/custom/js/&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
The Joomla API&#039;s [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration], [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] and [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] methods embed JavaScript into Joomla&#039;s index.php via the jdoc head tag:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;jdoc:include type=&amp;quot;head&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] or [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] methods to embed JavaScript includes would result in the index.php rendering the following HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;script type=&amp;quot;text/javaScript&amp;quot; src=&amp;quot;/media/system/js/sample.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Calling the class method [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration] would render the following HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;script type=&amp;quot;text/javaScript&amp;quot;&amp;gt;&lt;br /&gt;
window.addEvent(&amp;quot;domready&amp;quot;, function() {&lt;br /&gt;
    alert(&amp;quot;Embedded block of JS here&amp;quot;);&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/script&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using these methods is highly recommended as it clearly differentiates another scripting language (JavaScript) from the main PHP code, ensures all JavaScript is correctly embedded between the &amp;lt;head&amp;gt;&amp;lt;/head&amp;gt; tags and, in the case of JDocument::addScript and JHTML::script ensures that a JavaScript file is included only once (I.e. there is no .js file duplication).&lt;br /&gt;
&lt;br /&gt;
== Using a JavaScript Framework ==&lt;br /&gt;
&lt;br /&gt;
A Javascript framework provides developers with generic functionality for handling various coding tasks in a familiar, consistent and platform-independent way. A framework enables the developer to forget about the intricacies of implementing a certain function in different web browsers and focus on the requirements of the software.&lt;br /&gt;
&lt;br /&gt;
Two Javascript frameworks are provided as part of Joomla 3.0; JQuery and Mootools. JQuery is a newly introduced framework which integrates with Joomla&#039;s new Bootstrap HTML framework; Mootools is Joomla&#039;s legacy Javascript library which is now superseded by JQuery and is included for backwards compatibility with 3rd party extensions.&lt;br /&gt;
&lt;br /&gt;
In nearly all cases you should use a framework when developing Javascript in your extensions or templates and including one is very simple with Joomla&#039;s API.&lt;br /&gt;
&lt;br /&gt;
=== JQuery ===&lt;br /&gt;
&lt;br /&gt;
As of Joomla 3.0 it is highly recommended you use the JQuery framework for all your Javascript as it is loaded by nearly every template which uses the Bootstrap HTML/CSS framework. In those few instances where JQuery is not available, you can easily include the framework using the JHTMLJQuery class&#039; framework method.&lt;br /&gt;
&lt;br /&gt;
To include the JQuery framework in your extension, you add the following code to your view.html.php or tmpl file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
JHtml::_(&#039;jquery.framework&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using the framework method ensures JQuery is included correctly, only once, and ensures it doesn&#039;t clash with Mootools (if also loaded).&lt;br /&gt;
&lt;br /&gt;
Then it is just a matter of leveraging the JQuery framework:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
JFactory::getDocument()-&amp;gt;addScriptDeclaration(&#039;&lt;br /&gt;
(function($) {&lt;br /&gt;
    $(document).ready(function() {&lt;br /&gt;
        alert($(&amp;quot;#list li&amp;quot;).size());&lt;br /&gt;
    });&lt;br /&gt;
})(jQuery);&lt;br /&gt;
&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Mootools ===&lt;br /&gt;
&lt;br /&gt;
Unless you are maintaining Javascript code which leverages Mootools or you are developing an extension for a Joomla version prior to 3.0 it is recommended you use JQuery instead.&lt;br /&gt;
&lt;br /&gt;
Firstly, you will need to include the Mootools code in your extension. To include the Mootools framework in your extension, you add the following code to your view.html.php or tmpl file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
JHtml::_(&#039;behavior.framework&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code results in the same outcome as the similar JQuery framework statement; that is it ensures Mootools is included correctly and only once.&lt;br /&gt;
&lt;br /&gt;
Then using Mootools is almost identical to JQuery:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
JFactory::getDocument()-&amp;gt;addScriptDeclaration(&#039;&lt;br /&gt;
window.addEvent(&#039;domready&#039;, function() {&lt;br /&gt;
    alert($(&amp;quot;list&amp;quot;).getElements(&amp;quot;li&amp;quot;).length);&lt;br /&gt;
});&lt;br /&gt;
&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For more information, see [[#Further Reading]]&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
&lt;br /&gt;
[[JHtmlBehavior::framework/11.1| JHtmlBehaviour::framework method]]&lt;br /&gt;
&lt;br /&gt;
[[Ajax using MooTools]]&lt;br /&gt;
&lt;br /&gt;
[[Adding Javascript moo.fx to your component]]&lt;br /&gt;
&lt;br /&gt;
== External Links ==&lt;br /&gt;
&lt;br /&gt;
http://en.wikipedia.org/wiki/JavaScript&lt;br /&gt;
&lt;br /&gt;
http://www.w3schools.com/js/default.asp&lt;br /&gt;
&lt;br /&gt;
http://mootools.net/&lt;br /&gt;
&lt;br /&gt;
http://jquery.com/&lt;br /&gt;
&lt;br /&gt;
[[Category:Templates]]&lt;br /&gt;
[[Category:Modules]]&lt;br /&gt;
[[Category:Components]]&lt;br /&gt;
[[Category:Plugins]]&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>Haydenyoung</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Adding_JavaScript&amp;diff=76236</id>
		<title>Adding JavaScript</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Adding_JavaScript&amp;diff=76236"/>
		<updated>2012-10-08T06:06:28Z</updated>

		<summary type="html">&lt;p&gt;Haydenyoung: Introduction to javascript frameworks.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{cookiejar}}&lt;br /&gt;
&lt;br /&gt;
JavaScript (also known as ECMAScript) is a scripting language primarily used in client-side web site development to extend and enhance an end-user&#039;s experience. Joomla provides developers with easy-to-use mechanisms to include JavaScript in their extensions using existing API methods.&lt;br /&gt;
&lt;br /&gt;
There are a number of methods for including JavaScript in your Joomla extensions; some of these are described below.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
&lt;br /&gt;
There are three methods for embedding JavaScript into your code using the Joomla API; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration], [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] and [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script]. These methods should be called either in your component&#039;s View class (&amp;lt;yourcomponent&amp;gt;/views/&amp;lt;yourview&amp;gt;/view.html.php) or template script (&amp;lt;yourcomponent&amp;gt;/views/&amp;lt;yourview&amp;gt;/tmpl/&amp;lt;yourtemplate&amp;gt;.php or in the case of a module, in its template script (&amp;lt;yourmodule&amp;gt;/tmpl/&amp;lt;yourtemplate&amp;gt;.php). &lt;br /&gt;
&lt;br /&gt;
===Inline JavaScript===&lt;br /&gt;
&lt;br /&gt;
Blocks of JavaScript code can be declared directly within a component or module&#039;s display template using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html JDocument] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration addScriptDeclaration] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$document = &amp;amp;JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addScriptDeclaration(&#039;&lt;br /&gt;
    window.event(&amp;quot;domready&amp;quot;, function() {&lt;br /&gt;
        alert(&amp;quot;An inline JavaScript Declaration&amp;quot;);&lt;br /&gt;
    });&lt;br /&gt;
&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===External JavaScript===&lt;br /&gt;
&lt;br /&gt;
Alternatively, you may wish to separate your JavaScript into a separate file. Separating your JavaScript into an external file can make your template code easier to read especially if the JavaScript is lengthy or complex.&lt;br /&gt;
&lt;br /&gt;
There are two ways to include a JavaScript file using the Joomla API. The first involves using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html JDocument] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript addScript] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$document = &amp;amp;JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addScript(&#039;/media/system/js/sample.js&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The second uses the [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html JHTML] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// Add the path parameter if the path is different than &#039;media/system/js/&#039;&lt;br /&gt;
JHTML::script(&#039;sample.js&#039;, &#039;templates/custom/js/&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
The Joomla API&#039;s [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration], [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] and [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] methods embed JavaScript into Joomla&#039;s index.php via the jdoc head tag:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;jdoc:include type=&amp;quot;head&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] or [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] methods to embed JavaScript includes would result in the index.php rendering the following HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;script type=&amp;quot;text/javaScript&amp;quot; src=&amp;quot;/media/system/js/sample.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Calling the class method [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration] would render the following HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;script type=&amp;quot;text/javaScript&amp;quot;&amp;gt;&lt;br /&gt;
window.addEvent(&amp;quot;domready&amp;quot;, function() {&lt;br /&gt;
    alert(&amp;quot;Embedded block of JS here&amp;quot;);&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/script&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using these methods is highly recommended as it clearly differentiates another scripting language (JavaScript) from the main PHP code, ensures all JavaScript is correctly embedded between the &amp;lt;head&amp;gt;&amp;lt;/head&amp;gt; tags and, in the case of JDocument::addScript and JHTML::script ensures that a JavaScript file is included only once (I.e. there is no .js file duplication).&lt;br /&gt;
&lt;br /&gt;
== Using a JavaScript Framework ==&lt;br /&gt;
&lt;br /&gt;
A Javascript framework provides developers with generic functionality for handling various coding tasks in a familiar, consistent and platform-independent way. A framework enables the developer to forget about the intricacies of implementing a certain function in different web browsers and focus on the requirements of the software.&lt;br /&gt;
&lt;br /&gt;
Two Javascript frameworks are provided as part of Joomla 3.0; JQuery and Mootools. JQuery is a newly introduced framework which integrates with Joomla&#039;s new Bootstrap HTML framework; Mootools is Joomla&#039;s legacy Javascript library which is now superseded by JQuery and is included for backwards compatibility with 3rd party extensions.&lt;br /&gt;
&lt;br /&gt;
In nearly all cases you should use a framework when developing Javascript in your extensions or templates and including one is very simple with Joomla&#039;s API.&lt;br /&gt;
&lt;br /&gt;
=== JQuery ===&lt;br /&gt;
&lt;br /&gt;
As of Joomla 3.0 it is highly recommended you use the JQuery framework for all your Javascript as it is loaded by nearly every template which uses the Bootstrap HTML/CSS framework. In those few instances where JQuery is not available, you can easily include the framework using the JHTMLJQuery class&#039; framework method.&lt;br /&gt;
&lt;br /&gt;
To include the JQuery framework in your extension, you add the following code to your view.html.php or tmpl file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
JHtml::_(&#039;jquery.framework&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using the framework method ensures JQuery is included correctly, only once, and ensures it doesn&#039;t clash with Mootools (if also loaded).&lt;br /&gt;
&lt;br /&gt;
Then it is just a matter of leveraging the JQuery framework:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
JFactory::getDocument()-&amp;gt;addScriptDeclaration(&#039;&lt;br /&gt;
(function($) {&lt;br /&gt;
    $(document).ready(function() {&lt;br /&gt;
        alert($(&amp;quot;#list li&amp;quot;).size());&lt;br /&gt;
    });&lt;br /&gt;
})(jQuery);&lt;br /&gt;
&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Mootools ===&lt;br /&gt;
&lt;br /&gt;
Unless you are maintaining Javascript code which leverages Mootools or you are developing an extension for a Joomla version prior to 3.0 it is recommended you use JQuery instead.&lt;br /&gt;
&lt;br /&gt;
Firstly, you will need to include the Mootools code in your extension. To include the Mootools framework in your extension, you add the following code to your view.html.php or tmpl file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
JHtml::_(&#039;behavior.framework&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code results in the same outcome as the similar JQuery framework statement; that is it ensures Mootools is included correctly and only once.&lt;br /&gt;
&lt;br /&gt;
Then using Mootools is almost identical to JQuery:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
JFactory::getDocument()-&amp;gt;addScriptDeclaration(&#039;&lt;br /&gt;
window.addEvent(&#039;domready&#039;, function() {&lt;br /&gt;
    alert($(&amp;quot;list&amp;quot;).getElements(&amp;quot;li&amp;quot;).length);&lt;br /&gt;
});&lt;br /&gt;
&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For more information, see [[#Further Reading]]&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
&lt;br /&gt;
[[Category:Templates]]&lt;br /&gt;
[[Category:Modules]]&lt;br /&gt;
[[Category:Components]]&lt;br /&gt;
[[Category:Plugins]]&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>Haydenyoung</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Adding_JavaScript&amp;diff=75275</id>
		<title>Adding JavaScript</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Adding_JavaScript&amp;diff=75275"/>
		<updated>2012-09-17T23:02:46Z</updated>

		<summary type="html">&lt;p&gt;Haydenyoung: See also heading&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{cookiejar}}&lt;br /&gt;
&lt;br /&gt;
JavaScript (also known as ECMAScript) is a scripting language primarily used in client-side web site development to extend and enhance an end-user&#039;s experience. Joomla provides developers with easy-to-use mechanisms to include JavaScript in their extensions using existing API methods.&lt;br /&gt;
&lt;br /&gt;
There are a number of methods for including JavaScript in your Joomla extensions; some of these are described below.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
&lt;br /&gt;
There are three methods for embedding JavaScript into your code using the Joomla API; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration], [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] and [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script]. These methods should be called either in your component&#039;s View class (&amp;lt;yourcomponent&amp;gt;/views/&amp;lt;yourview&amp;gt;/view.html.php) or template script (&amp;lt;yourcomponent&amp;gt;/views/&amp;lt;yourview&amp;gt;/tmpl/&amp;lt;yourtemplate&amp;gt;.php or in the case of a module, in its template script (&amp;lt;yourmodule&amp;gt;/tmpl/&amp;lt;yourtemplate&amp;gt;.php). &lt;br /&gt;
&lt;br /&gt;
===Inline JavaScript===&lt;br /&gt;
&lt;br /&gt;
Blocks of JavaScript code can be declared directly within a component or module&#039;s display template using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html JDocument] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration addScriptDeclaration] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$document = &amp;amp;JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addScriptDeclaration(&#039;&lt;br /&gt;
    window.event(&amp;quot;domready&amp;quot;, function() {&lt;br /&gt;
        alert(&amp;quot;An inline JavaScript Declaration&amp;quot;);&lt;br /&gt;
    });&lt;br /&gt;
&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===External JavaScript===&lt;br /&gt;
&lt;br /&gt;
Alternatively, you may wish to separate your JavaScript into a separate file. Separating your JavaScript into an external file can make your template code easier to read especially if the JavaScript is lengthy or complex.&lt;br /&gt;
&lt;br /&gt;
There are two ways to include a JavaScript file using the Joomla API. The first involves using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html JDocument] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript addScript] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$document = &amp;amp;JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addScript(&#039;/media/system/js/sample.js&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The second uses the [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html JHTML] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// Add the path parameter if the path is different than &#039;media/system/js/&#039;&lt;br /&gt;
JHTML::script(&#039;sample.js&#039;, &#039;templates/custom/js/&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
The Joomla API&#039;s [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration], [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] and [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] methods embed JavaScript into Joomla&#039;s index.php via the jdoc head tag:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;jdoc:include type=&amp;quot;head&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] or [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] methods to embed JavaScript includes would result in the index.php rendering the following HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;script type=&amp;quot;text/javaScript&amp;quot; src=&amp;quot;/media/system/js/sample.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Calling the class method [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration] would render the following HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;script type=&amp;quot;text/javaScript&amp;quot;&amp;gt;&lt;br /&gt;
window.addEvent(&amp;quot;domready&amp;quot;, function() {&lt;br /&gt;
    alert(&amp;quot;Embedded block of JS here&amp;quot;);&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/script&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using these methods is highly recommended as it clearly differentiates another scripting language (JavaScript) from the main PHP code, ensures all JavaScript is correctly embedded between the &amp;lt;head&amp;gt;&amp;lt;/head&amp;gt; tags and, in the case of JDocument::addScript and JHTML::script ensures that a JavaScript file is included only once (I.e. there is no .js file duplication).&lt;br /&gt;
&lt;br /&gt;
== Using JavaScript Libraries ==&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
&lt;br /&gt;
[[Category:Templates]]&lt;br /&gt;
[[Category:Modules]]&lt;br /&gt;
[[Category:Components]]&lt;br /&gt;
[[Category:Plugins]]&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>Haydenyoung</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Adding_JavaScript&amp;diff=75274</id>
		<title>Adding JavaScript</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Adding_JavaScript&amp;diff=75274"/>
		<updated>2012-09-17T23:01:28Z</updated>

		<summary type="html">&lt;p&gt;Haydenyoung: Further reading heading.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{cookiejar}}&lt;br /&gt;
&lt;br /&gt;
JavaScript (also known as ECMAScript) is a scripting language primarily used in client-side web site development to extend and enhance an end-user&#039;s experience. Joomla provides developers with easy-to-use mechanisms to include JavaScript in their extensions using existing API methods.&lt;br /&gt;
&lt;br /&gt;
There are a number of methods for including JavaScript in your Joomla extensions; some of these are described below.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
&lt;br /&gt;
There are three methods for embedding JavaScript into your code using the Joomla API; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration], [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] and [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script]. These methods should be called either in your component&#039;s View class (&amp;lt;yourcomponent&amp;gt;/views/&amp;lt;yourview&amp;gt;/view.html.php) or template script (&amp;lt;yourcomponent&amp;gt;/views/&amp;lt;yourview&amp;gt;/tmpl/&amp;lt;yourtemplate&amp;gt;.php or in the case of a module, in its template script (&amp;lt;yourmodule&amp;gt;/tmpl/&amp;lt;yourtemplate&amp;gt;.php). &lt;br /&gt;
&lt;br /&gt;
===Inline JavaScript===&lt;br /&gt;
&lt;br /&gt;
Blocks of JavaScript code can be declared directly within a component or module&#039;s display template using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html JDocument] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration addScriptDeclaration] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$document = &amp;amp;JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addScriptDeclaration(&#039;&lt;br /&gt;
    window.event(&amp;quot;domready&amp;quot;, function() {&lt;br /&gt;
        alert(&amp;quot;An inline JavaScript Declaration&amp;quot;);&lt;br /&gt;
    });&lt;br /&gt;
&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===External JavaScript===&lt;br /&gt;
&lt;br /&gt;
Alternatively, you may wish to separate your JavaScript into a separate file. Separating your JavaScript into an external file can make your template code easier to read especially if the JavaScript is lengthy or complex.&lt;br /&gt;
&lt;br /&gt;
There are two ways to include a JavaScript file using the Joomla API. The first involves using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html JDocument] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript addScript] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$document = &amp;amp;JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addScript(&#039;/media/system/js/sample.js&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The second uses the [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html JHTML] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// Add the path parameter if the path is different than &#039;media/system/js/&#039;&lt;br /&gt;
JHTML::script(&#039;sample.js&#039;, &#039;templates/custom/js/&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
The Joomla API&#039;s [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration], [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] and [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] methods embed JavaScript into Joomla&#039;s index.php via the jdoc head tag:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;jdoc:include type=&amp;quot;head&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] or [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] methods to embed JavaScript includes would result in the index.php rendering the following HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;script type=&amp;quot;text/javaScript&amp;quot; src=&amp;quot;/media/system/js/sample.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Calling the class method [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration] would render the following HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;script type=&amp;quot;text/javaScript&amp;quot;&amp;gt;&lt;br /&gt;
window.addEvent(&amp;quot;domready&amp;quot;, function() {&lt;br /&gt;
    alert(&amp;quot;Embedded block of JS here&amp;quot;);&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/script&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using these methods is highly recommended as it clearly differentiates another scripting language (JavaScript) from the main PHP code, ensures all JavaScript is correctly embedded between the &amp;lt;head&amp;gt;&amp;lt;/head&amp;gt; tags and, in the case of JDocument::addScript and JHTML::script ensures that a JavaScript file is included only once (I.e. there is no .js file duplication).&lt;br /&gt;
&lt;br /&gt;
== Using JavaScript Libraries ==&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
&lt;br /&gt;
[[Category:Templates]]&lt;br /&gt;
[[Category:Modules]]&lt;br /&gt;
[[Category:Components]]&lt;br /&gt;
[[Category:Plugins]]&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>Haydenyoung</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Adding_JavaScript&amp;diff=75273</id>
		<title>Adding JavaScript</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Adding_JavaScript&amp;diff=75273"/>
		<updated>2012-09-17T23:01:01Z</updated>

		<summary type="html">&lt;p&gt;Haydenyoung: New heading for covering Mootools. Keeping the heading generic for upcoming JQuery update.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{cookiejar}}&lt;br /&gt;
&lt;br /&gt;
JavaScript (also known as ECMAScript) is a scripting language primarily used in client-side web site development to extend and enhance an end-user&#039;s experience. Joomla provides developers with easy-to-use mechanisms to include JavaScript in their extensions using existing API methods.&lt;br /&gt;
&lt;br /&gt;
There are a number of methods for including JavaScript in your Joomla extensions; some of these are described below.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
&lt;br /&gt;
There are three methods for embedding JavaScript into your code using the Joomla API; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration], [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] and [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script]. These methods should be called either in your component&#039;s View class (&amp;lt;yourcomponent&amp;gt;/views/&amp;lt;yourview&amp;gt;/view.html.php) or template script (&amp;lt;yourcomponent&amp;gt;/views/&amp;lt;yourview&amp;gt;/tmpl/&amp;lt;yourtemplate&amp;gt;.php or in the case of a module, in its template script (&amp;lt;yourmodule&amp;gt;/tmpl/&amp;lt;yourtemplate&amp;gt;.php). &lt;br /&gt;
&lt;br /&gt;
===Inline JavaScript===&lt;br /&gt;
&lt;br /&gt;
Blocks of JavaScript code can be declared directly within a component or module&#039;s display template using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html JDocument] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration addScriptDeclaration] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$document = &amp;amp;JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addScriptDeclaration(&#039;&lt;br /&gt;
    window.event(&amp;quot;domready&amp;quot;, function() {&lt;br /&gt;
        alert(&amp;quot;An inline JavaScript Declaration&amp;quot;);&lt;br /&gt;
    });&lt;br /&gt;
&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===External JavaScript===&lt;br /&gt;
&lt;br /&gt;
Alternatively, you may wish to separate your JavaScript into a separate file. Separating your JavaScript into an external file can make your template code easier to read especially if the JavaScript is lengthy or complex.&lt;br /&gt;
&lt;br /&gt;
There are two ways to include a JavaScript file using the Joomla API. The first involves using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html JDocument] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript addScript] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$document = &amp;amp;JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addScript(&#039;/media/system/js/sample.js&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The second uses the [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html JHTML] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// Add the path parameter if the path is different than &#039;media/system/js/&#039;&lt;br /&gt;
JHTML::script(&#039;sample.js&#039;, &#039;templates/custom/js/&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
The Joomla API&#039;s [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration], [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] and [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] methods embed JavaScript into Joomla&#039;s index.php via the jdoc head tag:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;jdoc:include type=&amp;quot;head&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] or [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] methods to embed JavaScript includes would result in the index.php rendering the following HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;script type=&amp;quot;text/javaScript&amp;quot; src=&amp;quot;/media/system/js/sample.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Calling the class method [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration] would render the following HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;script type=&amp;quot;text/javaScript&amp;quot;&amp;gt;&lt;br /&gt;
window.addEvent(&amp;quot;domready&amp;quot;, function() {&lt;br /&gt;
    alert(&amp;quot;Embedded block of JS here&amp;quot;);&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/script&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using these methods is highly recommended as it clearly differentiates another scripting language (JavaScript) from the main PHP code, ensures all JavaScript is correctly embedded between the &amp;lt;head&amp;gt;&amp;lt;/head&amp;gt; tags and, in the case of JDocument::addScript and JHTML::script ensures that a JavaScript file is included only once (I.e. there is no .js file duplication).&lt;br /&gt;
&lt;br /&gt;
== Using JavaScript Libraries ==&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:Templates]]&lt;br /&gt;
[[Category:Modules]]&lt;br /&gt;
[[Category:Components]]&lt;br /&gt;
[[Category:Plugins]]&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>Haydenyoung</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Adding_JavaScript&amp;diff=75272</id>
		<title>Adding JavaScript</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Adding_JavaScript&amp;diff=75272"/>
		<updated>2012-09-17T22:59:39Z</updated>

		<summary type="html">&lt;p&gt;Haydenyoung: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{cookiejar}}&lt;br /&gt;
&lt;br /&gt;
JavaScript (also known as ECMAScript) is a scripting language primarily used in client-side web site development to extend and enhance an end-user&#039;s experience. Joomla provides developers with easy-to-use mechanisms to include JavaScript in their extensions using existing API methods.&lt;br /&gt;
&lt;br /&gt;
There are a number of methods for including JavaScript in your Joomla extensions; some of these are described below.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
&lt;br /&gt;
There are three methods for embedding JavaScript into your code using the Joomla API; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration], [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] and [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script]. These methods should be called either in your component&#039;s View class (&amp;lt;yourcomponent&amp;gt;/views/&amp;lt;yourview&amp;gt;/view.html.php) or template script (&amp;lt;yourcomponent&amp;gt;/views/&amp;lt;yourview&amp;gt;/tmpl/&amp;lt;yourtemplate&amp;gt;.php or in the case of a module, in its template script (&amp;lt;yourmodule&amp;gt;/tmpl/&amp;lt;yourtemplate&amp;gt;.php). &lt;br /&gt;
&lt;br /&gt;
===Inline JavaScript===&lt;br /&gt;
&lt;br /&gt;
Blocks of JavaScript code can be declared directly within a component or module&#039;s display template using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html JDocument] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration addScriptDeclaration] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$document = &amp;amp;JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addScriptDeclaration(&#039;&lt;br /&gt;
    window.event(&amp;quot;domready&amp;quot;, function() {&lt;br /&gt;
        alert(&amp;quot;An inline JavaScript Declaration&amp;quot;);&lt;br /&gt;
    });&lt;br /&gt;
&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===External JavaScript===&lt;br /&gt;
&lt;br /&gt;
Alternatively, you may wish to separate your JavaScript into a separate file. Separating your JavaScript into an external file can make your template code easier to read especially if the JavaScript is lengthy or complex.&lt;br /&gt;
&lt;br /&gt;
There are two ways to include a JavaScript file using the Joomla API. The first involves using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html JDocument] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript addScript] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$document = &amp;amp;JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addScript(&#039;/media/system/js/sample.js&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The second uses the [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html JHTML] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// Add the path parameter if the path is different than &#039;media/system/js/&#039;&lt;br /&gt;
JHTML::script(&#039;sample.js&#039;, &#039;templates/custom/js/&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
The Joomla API&#039;s [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration], [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] and [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] methods embed JavaScript into Joomla&#039;s index.php via the jdoc head tag:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;jdoc:include type=&amp;quot;head&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] or [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] methods to embed JavaScript includes would result in the index.php rendering the following HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;script type=&amp;quot;text/javaScript&amp;quot; src=&amp;quot;/media/system/js/sample.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Calling the class method [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration] would render the following HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;script type=&amp;quot;text/javaScript&amp;quot;&amp;gt;&lt;br /&gt;
window.addEvent(&amp;quot;domready&amp;quot;, function() {&lt;br /&gt;
    alert(&amp;quot;Embedded block of JS here&amp;quot;);&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/script&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using these methods is highly recommended as it clearly differentiates another scripting language (JavaScript) from the main PHP code, ensures all JavaScript is correctly embedded between the &amp;lt;head&amp;gt;&amp;lt;/head&amp;gt; tags and, in the case of JDocument::addScript and JHTML::script ensures that a JavaScript file is included only once (I.e. there is no .js file duplication).&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:Templates]]&lt;br /&gt;
[[Category:Modules]]&lt;br /&gt;
[[Category:Components]]&lt;br /&gt;
[[Category:Plugins]]&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>Haydenyoung</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Adding_JavaScript&amp;diff=75271</id>
		<title>Adding JavaScript</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Adding_JavaScript&amp;diff=75271"/>
		<updated>2012-09-17T22:55:27Z</updated>

		<summary type="html">&lt;p&gt;Haydenyoung: Keep spelling of JavaScript same throughout document.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{cookiejar}}&lt;br /&gt;
&lt;br /&gt;
JavaScript (also known as ECMAScript) is a scripting language primarily used in client-side web site development to extend and enhance an end-user&#039;s experience. Joomla provides developers with easy-to-use mechanisms to include JavaScript in their extensions using existing API methods.&lt;br /&gt;
&lt;br /&gt;
There are a number of methods for including JavaScript in your Joomla extensions; some of these are described below.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
&lt;br /&gt;
There are three methods for embedding JavaScript into your code using the Joomla API; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration], [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] and [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script]. These methods should be called either in your component&#039;s View class (&amp;lt;yourcomponent&amp;gt;/views/&amp;lt;yourview&amp;gt;/view.html.php) or template script (&amp;lt;yourcomponent&amp;gt;/views/&amp;lt;yourview&amp;gt;/tmpl/&amp;lt;yourtemplate&amp;gt;.php or in the case of a module, in its template script (&amp;lt;yourmodule&amp;gt;/tmpl/&amp;lt;yourtemplate&amp;gt;.php). &lt;br /&gt;
&lt;br /&gt;
===Inline JavaScript===&lt;br /&gt;
&lt;br /&gt;
Blocks of JavaScript code can be declared directly within a component or module&#039;s display template using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html JDocument] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration addScriptDeclaration] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$document = &amp;amp;JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addScriptDeclaration(&#039;&lt;br /&gt;
    window.event(&amp;quot;domready&amp;quot;, function() {&lt;br /&gt;
        alert(&amp;quot;An inline JavaScript Declaration&amp;quot;);&lt;br /&gt;
    });&lt;br /&gt;
&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===External JavaScript===&lt;br /&gt;
&lt;br /&gt;
Alternatively, you may wish to separate your JavaScript into a separate file. Separating your JavaScript into an external file can make your template code easier to read especially if the JavaScript is lengthy or complex.&lt;br /&gt;
&lt;br /&gt;
There are two ways to include a JavaScript file using the Joomla API. The first involves using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html JDocument] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript addScript] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$document = &amp;amp;JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addScript(&#039;/media/system/js/sample.js&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The second uses the [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html JHTML] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// Add the path parameter if the path is different than &#039;media/system/js/&#039;&lt;br /&gt;
JHTML::script(&#039;sample.js&#039;, &#039;templates/custom/js/&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
The Joomla API&#039;s [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration], [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] and [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] methods embed JavaScript into Joomla&#039;s index.php via the jdoc head tag:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;jdoc:include type=&amp;quot;head&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] or [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] methods to embed JavaScript includes would result in the index.php rendering the following HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;script type=&amp;quot;text/javaScript&amp;quot; src=&amp;quot;/media/system/js/sample.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Calling the class method [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration] would render the following HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;script type=&amp;quot;text/javaScript&amp;quot;&amp;gt;&lt;br /&gt;
window.addEvent(&amp;quot;domready&amp;quot;, function() {&lt;br /&gt;
    alert(&amp;quot;Embedded block of JS here&amp;quot;);&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/script&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using these methods is highly recommended as it clearly differentiates another scripting language (JavaScript) from the main PHP code, ensures all JavaScript is correctly embedded between the &amp;lt;head&amp;gt;&amp;lt;/head&amp;gt; tags and, in the case of JDocument::addScript and JHTML::script ensures that a JavaScript file is included only once (I.e. there is no .js file duplication).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Please Note:&#039;&#039;&#039; Joomla 1.6+ may handle MooTools differently than in previous versions. &amp;lt;ref&amp;gt;[http://forum.joomla.org/viewtopic.php?f=502&amp;amp;t=273960 Whitepaper] Upgrade to mootools 1.2&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:Templates]]&lt;br /&gt;
[[Category:Modules]]&lt;br /&gt;
[[Category:Components]]&lt;br /&gt;
[[Category:Plugins]]&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>Haydenyoung</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Adding_JavaScript&amp;diff=75270</id>
		<title>Adding JavaScript</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Adding_JavaScript&amp;diff=75270"/>
		<updated>2012-09-17T22:53:43Z</updated>

		<summary type="html">&lt;p&gt;Haydenyoung: Usage introduction.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{cookiejar}}&lt;br /&gt;
&lt;br /&gt;
Javascript (also known as ECMAScript) is a scripting language primarily used in client-side web site development to extend and enhance an end-user&#039;s experience. Joomla provides developers with easy-to-use mechanisms to include Javascript in their extensions using existing API methods.&lt;br /&gt;
&lt;br /&gt;
There are a number of methods for including Javascript in your Joomla extensions; some of these are described below.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
&lt;br /&gt;
There are three methods for embedding JavaScript into your code using the Joomla API; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration], [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] and [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script]. These methods should be called either in your component&#039;s View class (&amp;lt;yourcomponent&amp;gt;/views/&amp;lt;yourview&amp;gt;/view.html.php) or template script (&amp;lt;yourcomponent&amp;gt;/views/&amp;lt;yourview&amp;gt;/tmpl/&amp;lt;yourtemplate&amp;gt;.php or in the case of a module, in its template script (&amp;lt;yourmodule&amp;gt;/tmpl/&amp;lt;yourtemplate&amp;gt;.php). &lt;br /&gt;
&lt;br /&gt;
===Inline Javascript===&lt;br /&gt;
&lt;br /&gt;
Blocks of Javascript code can be declared directly within a component or module&#039;s display template using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html JDocument] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration addScriptDeclaration] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$document = &amp;amp;JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addScriptDeclaration(&#039;&lt;br /&gt;
    window.event(&amp;quot;domready&amp;quot;, function() {&lt;br /&gt;
        alert(&amp;quot;An inline Javascript Declaration&amp;quot;);&lt;br /&gt;
    });&lt;br /&gt;
&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===External Javascript===&lt;br /&gt;
&lt;br /&gt;
Alternatively, you may wish to separate your Javascript into a separate file. Separating your Javascript into an external file can make your template code easier to read especially if the Javascript is lengthy or complex.&lt;br /&gt;
&lt;br /&gt;
There are two ways to include a Javascript file using the Joomla API. The first involves using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html JDocument] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript addScript] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$document = &amp;amp;JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addScript(&#039;/media/system/js/sample.js&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The second uses the [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html JHTML] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// Add the path parameter if the path is different than &#039;media/system/js/&#039;&lt;br /&gt;
JHTML::script(&#039;sample.js&#039;, &#039;templates/custom/js/&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
The Joomla API&#039;s [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration], [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] and [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] methods embed Javascript into Joomla&#039;s index.php via the jdoc head tag:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;jdoc:include type=&amp;quot;head&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] or [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] methods to embed Javascript includes would result in the index.php rendering the following HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;/media/system/js/sample.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Calling the class method [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration] would render the following HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;&lt;br /&gt;
window.addEvent(&amp;quot;domready&amp;quot;, function() {&lt;br /&gt;
    alert(&amp;quot;Embedded block of JS here&amp;quot;);&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/script&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using these methods is highly recommended as it clearly differentiates another scripting language (Javascript) from the main PHP code, ensures all Javascript is correctly embedded between the &amp;lt;head&amp;gt;&amp;lt;/head&amp;gt; tags and, in the case of JDocument::addScript and JHTML::script ensures that a Javascript file is included only once (I.e. there is no .js file duplication).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Please Note:&#039;&#039;&#039; Joomla 1.6+ may handle MooTools differently than in previous versions. &amp;lt;ref&amp;gt;[http://forum.joomla.org/viewtopic.php?f=502&amp;amp;t=273960 Whitepaper] Upgrade to mootools 1.2&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:Templates]]&lt;br /&gt;
[[Category:Modules]]&lt;br /&gt;
[[Category:Components]]&lt;br /&gt;
[[Category:Plugins]]&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>Haydenyoung</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Adding_JavaScript&amp;diff=75244</id>
		<title>Adding JavaScript</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Adding_JavaScript&amp;diff=75244"/>
		<updated>2012-09-17T13:19:42Z</updated>

		<summary type="html">&lt;p&gt;Haydenyoung: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{cookiejar}}&lt;br /&gt;
&lt;br /&gt;
Javascript (also known as ECMAScript) is a scripting language primarily used in client-side web site development to extend and enhance an end-user&#039;s experience. Joomla provides developers with easy-to-use mechanisms to include Javascript in their extensions using existing API methods.&lt;br /&gt;
&lt;br /&gt;
There are a number of methods for including Javascript in your Joomla extensions; some of these are described below.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
&lt;br /&gt;
===Inline Javascript===&lt;br /&gt;
&lt;br /&gt;
Blocks of Javascript code can be declared directly within a component or module&#039;s display template using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html JDocument] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration addScriptDeclaration] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$document = &amp;amp;JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addScriptDeclaration(&#039;&lt;br /&gt;
    window.event(&amp;quot;domready&amp;quot;, function() {&lt;br /&gt;
        alert(&amp;quot;An inline Javascript Declaration&amp;quot;);&lt;br /&gt;
    });&lt;br /&gt;
&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===External Javascript===&lt;br /&gt;
&lt;br /&gt;
Alternatively, you may wish to separate your Javascript into a separate file. Separating your Javascript into an external file can make your template code easier to read especially if the Javascript is lengthy or complex.&lt;br /&gt;
&lt;br /&gt;
There are two ways to include a Javascript file using the Joomla API. The first involves using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html JDocument] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript addScript] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$document = &amp;amp;JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addScript(&#039;/media/system/js/sample.js&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The second uses the [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html JHTML] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// Add the path parameter if the path is different than &#039;media/system/js/&#039;&lt;br /&gt;
JHTML::script(&#039;sample.js&#039;, &#039;templates/custom/js/&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
The Joomla API&#039;s [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration], [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] and [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] methods embed Javascript into Joomla&#039;s index.php via the jdoc head tag:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;jdoc:include type=&amp;quot;head&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] or [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] methods to embed Javascript includes would result in the index.php rendering the following HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;/media/system/js/sample.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Calling the class method [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration] would render the following HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;&lt;br /&gt;
window.addEvent(&amp;quot;domready&amp;quot;, function() {&lt;br /&gt;
    alert(&amp;quot;Embedded block of JS here&amp;quot;);&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/script&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using these methods is highly recommended as it clearly differentiates another scripting language (Javascript) from the main PHP code, ensures all Javascript is correctly embedded between the &amp;lt;head&amp;gt;&amp;lt;/head&amp;gt; tags and, in the case of JDocument::addScript and JHTML::script ensures that a Javascript file is included only once (I.e. there is no .js file duplication).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Please Note:&#039;&#039;&#039; Joomla 1.6+ may handle MooTools differently than in previous versions. &amp;lt;ref&amp;gt;[http://forum.joomla.org/viewtopic.php?f=502&amp;amp;t=273960 Whitepaper] Upgrade to mootools 1.2&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:Templates]]&lt;br /&gt;
[[Category:Modules]]&lt;br /&gt;
[[Category:Components]]&lt;br /&gt;
[[Category:Plugins]]&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>Haydenyoung</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Adding_JavaScript&amp;diff=75243</id>
		<title>Adding JavaScript</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Adding_JavaScript&amp;diff=75243"/>
		<updated>2012-09-17T13:10:01Z</updated>

		<summary type="html">&lt;p&gt;Haydenyoung: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{cookiejar}}&lt;br /&gt;
&lt;br /&gt;
Javascript (also known as ECMAScript) is a scripting language and is primarily used in client-side web site development to extend and enhance an end-user&#039;s experience. Joomla provides developers with easy-to-use mechanisms to include Javascript in their extensions using existing API methods.&lt;br /&gt;
&lt;br /&gt;
There are a number of methods for including Javascript in your Joomla extensions; some of these are described below.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
&lt;br /&gt;
===Inline Javascript===&lt;br /&gt;
&lt;br /&gt;
Blocks of Javascript code can be declared directly within a component or module&#039;s display template using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html JDocument] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration addScriptDeclaration] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$document = &amp;amp;JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addScriptDeclaration(&#039;&lt;br /&gt;
    window.event(&amp;quot;domready&amp;quot;, function() {&lt;br /&gt;
        alert(&amp;quot;An inline Javascript Declaration&amp;quot;);&lt;br /&gt;
    });&lt;br /&gt;
&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===External Javascript===&lt;br /&gt;
&lt;br /&gt;
Alternatively, you may wish to separate your Javascript into a separate file. Separating your Javascript into an external file can make your template code easier to read especially if the Javascript is lengthy or complex.&lt;br /&gt;
&lt;br /&gt;
There are two ways to include a Javascript file using the Joomla API. The first involves using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html JDocument] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript addScript] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$document = &amp;amp;JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addScript(&#039;/media/system/js/sample.js&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The second uses the [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html JHTML] class&#039; [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// Add the path parameter if the path is different than &#039;media/system/js/&#039;&lt;br /&gt;
JHTML::script(&#039;sample.js&#039;, &#039;templates/custom/js/&#039;);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
The Joomla API&#039;s [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration], [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] and [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] methods embed Javascript into Joomla&#039;s index.php via the jdoc head tag:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;jdoc:include type=&amp;quot;head&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using the [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScript JDocument::addScript] or [http://api.joomla.org/1.5/Joomla-Framework/HTML/JHTML.html#script script] methods to embed Javascript includes would result in the index.php rendering the following HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;/media/system/js/sample.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Calling the class method [http://api.joomla.org/1.5/Joomla-Framework/Document/JDocument.html#addScriptDeclaration JDocument::addScriptDeclaration] would render the following HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;&lt;br /&gt;
window.addEvent(&amp;quot;domready&amp;quot;, function() {&lt;br /&gt;
    alert(&amp;quot;Embedded block of JS here&amp;quot;);&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/script&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using these methods is highly recommended as it clearly differentiates another scripting language (Javascript) from the main PHP code, ensures all Javascript is correctly embedded between the &amp;lt;head&amp;gt;&amp;lt;/head&amp;gt; tags and, in the case of JDocument::addScript and JHTML::script ensures that a Javascript file is included only once (I.e. there is no .js file duplication).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Please Note:&#039;&#039;&#039; Joomla 1.6+ may handle MooTools differently than in previous versions. &amp;lt;ref&amp;gt;[http://forum.joomla.org/viewtopic.php?f=502&amp;amp;t=273960 Whitepaper] Upgrade to mootools 1.2&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:Templates]]&lt;br /&gt;
[[Category:Modules]]&lt;br /&gt;
[[Category:Components]]&lt;br /&gt;
[[Category:Plugins]]&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>Haydenyoung</name></author>
	</entry>
</feed>