Multiple mySQL Queries

For Joomla! 1.5 Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general
Forum rules
Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.
Locked
Armorblue
Joomla! Intern
Joomla! Intern
Posts: 88
Joined: Mon May 22, 2006 10:53 am

Multiple mySQL Queries

Post by Armorblue » Wed Feb 04, 2009 7:26 am

What is the best way to build and call multiple mySQL queries within Joomla? I have one single query working, but the app I'm building needs three total - two to pull information and one to insert. I understand that $query(2,3,4), etc, works in general. The problem with it is getting the results from the database class. For the first query, I'm using this:
$db->loadResult()
What should I do for the others?

shinta2
Joomla! Apprentice
Joomla! Apprentice
Posts: 33
Joined: Thu Mar 09, 2006 3:49 pm

Re: Multiple mySQL Queries

Post by shinta2 » Wed Feb 04, 2009 8:50 pm

$query = 'SELECT something FROM #__sometable';
$db->setQuery($query)
$results1 = $db->loadResult()

$query = 'SELECT somethingelse FROM #__anothertable';
$db->setQuery($query)
$results2 = $db->loadResult()

$query = 'INSERT INTO #__yourtable VALUES (yourvalues)";
$db->setQuery($query)
if ($db->query() === FALSE)
echo 'mysql error...';

etc...
Last edited by shinta2 on Sun Feb 08, 2009 7:19 am, edited 1 time in total.

Armorblue
Joomla! Intern
Joomla! Intern
Posts: 88
Joined: Mon May 22, 2006 10:53 am

Re: Multiple mySQL Queries

Post by Armorblue » Wed Feb 04, 2009 10:43 pm

Thank you soo much, that'll speed things up in the dev department.

The Tracoter
Joomla! Apprentice
Joomla! Apprentice
Posts: 20
Joined: Mon Jul 08, 2013 3:34 pm

Re: Multiple mySQL Queries

Post by The Tracoter » Tue Jul 16, 2013 12:56 pm

In my model (Joomla 2.5) this not work! Why? This is my sample

Code: Select all

<?php
	defined('_JEXEC') or die();
	
	jimport( 'joomla.application.component.modellist' );
	
	class AnagraficheModelServiziassociatiaggiuntivi extends JModelList
	{
		public function __construct($config = array())
		{
			if (empty($config['filter_fields'])) 
			{
				$config['filter_fields'] = array('id_servizi_aggiuntivi', 'id_persona', 'id_servizio', 'nome_servizio');
			}
		
			parent::__construct($config);
		}
		
		function getListQuery()
		{	
			/*
				SELECT id_servizi_aggiuntivi, id_persona , id_servizio , nome_servizio
				FROM joomlatest_servizi_aggiuntivi as serviziaggiuntivi, joomlatest_elenco_servizi
				WHERE cod_servizio=id_servizio
			*/
			/*
			$db = JFactory::getDBO();
			$query = $db->getQuery(true);
			$query->select('id_servizi_aggiuntivi, id_persona , id_servizio , nome_servizio');
			$query->from('#__servizi_aggiuntivi as serviziaggiuntivi, #__elenco_servizi');
			$query->where('cod_servizio=id_servizio');
			$result1 = $db->loadResult();
			*/
			
			/*
			$db->setQuery($query);
			$query->select('id_tipologia_socio, id_servizio as cod_servizio, nome_servizio');
			$query->from('#__associazione_servizi as serviziassociati, #__elenco_servizi');
			$query->where('cod_servizio=id_servizio');
			$result2 = $db->loadResult();
			*/
			$db = JFactory::getDBO();
			
			$query = 'SELECT id_servizi_aggiuntivi, id_persona , id_servizio , nome_servizio FROM #__servizi_aggiuntivi as serviziaggiuntivi, #__elenco_servizi WHERE cod_servizio=id_servizio';
			$db->setQuery($query);
			$result1 = $db->loadResult();
			
			$query = 'SELECT id_tipologia_socio, id_servizio as cod_servizio, nome_servizio FROM #__associazione_servizi as serviziassociati, #__elenco_servizi WHERE cod_servizio=id_servizio';
			$db->setQuery($query);
			$result2 = $db->loadResult(); 
		
			/*
			$search = $this->getState('filter.search');
			if (!empty($search)) 
			{
				$search = $db->Quote('%'.$db->escape($search, true).'%');
				$query->where('(serviziaggiuntivi.id_persona LIKE '.$search.')');
			}
			*/
			//$query->order($this->getState('list.ordering', 'id_persona').' '.$this->getState('list.direction', 'ASC'));
			//$row = $db->loadResult();
			
			return $query;
		}
		
		protected function populateState($ordering = null, $direction = null)
		{
			// Load the filter state.
			$search = $this->getUserStateFromRequest($this->context.'.filter.search', 'filter_search');
			$this->setState('filter.search', $search);

			// List state information.
			parent::populateState($ordering, $direction);
		}
	}
?>

TeonnynDev
Joomla! Apprentice
Joomla! Apprentice
Posts: 23
Joined: Thu Oct 18, 2012 2:54 pm

Re: Multiple mySQL Queries

Post by TeonnynDev » Tue Jul 16, 2013 6:15 pm

Please create your own posts in the future, this is an -ancient- one, I don't even have the ArmorBlue nickname anymore!


Locked

Return to “Joomla! 1.5 Coding”