joomla query
Today want to show on how to manipulate data in mysql basic steps.
Example 1 (SELECT statement)
$db =& JFactory::getDBO();
$query = “SELECT * FROM #__mytable”;
$db->setQuery( $query );
$db->query();
To retrieve 1 data from mysql use this code below:
$obj_row = $db->loadObject();
This will only return the objects. You need to use foreach to get the data.
For example:
foreach($obj_row as $a){
echo $a->id;
}
Tips: var_dump() would be an excellent assistance to know how the raw data is stored.
To load the lists of data from a table, we do not use $db->loadObject();. Instead we use $db->loadObjectList();
Example 2: To insert/update data
$db =& JFactory::getDBO();
$query = ” (update/insert mysql query statement)”;
$db->setQuery( $query );
$db->query();
OR
$__data =new stdClass();
$__data->id = null;
$__data->field1 = var1;
$__data->field2 = var2;
$__data->field3 = var3;
$__data->field4 = var4;
$__data->field5 = var5;
$db = JFactory::getDBO();
$db->insertObject( ‘#__mytable’, $__data, id ); (if you want to insert data)
$db->updateObject( ‘#__mytable’, $__data, id ); (if you want to insert data)
If we use insertObject/updateObject then we no need to put the mysql command, and the code would looks neater.
All the functions which are to manipulate data can be found in this class: JDatabase, which located at <root>/libraries/joomla/database/database.php







simple joomla query..