Can I do multiple queries?

madpad

Member
I am running into errors on this code, not sure what is missing here.

$dbcon = JFactory::getDbo();
$foodscores = $data['daily_logs_2___food_score'];
$mydata = $data['daily_logs_2___user_raw'];
$query = $dbcon->getQuery(true);
$query
->select('AVG(food_score) AS myavg_foodscore')
->from('daily_logs_2')
->where('user = '. $dbcon->quote($mydata));
$dbcon->setquery($query);
$myavg_foodscore = $dbcon->loadResult();

Can I run a second query right after the last line?

$query
->select('AVG(food_score) AS avg_foodscore')
->from('daily_logs_2')
$dbcon->setquery($query);
$avg_foodscore = $dbcon->loadResult();

Thank you for your assistance.
 
Hi, cannot see any obvious errors in your code.

Try dumping your variables and see if you get what you expect:
var_dump($foodscores);
exit;

And yes, you can run multiple queries one after another.
 
Try this:
PHP:
$dbcon = JFactory::getDbo();
$foodscores = $data['daily_logs_2___food_score'];
$mydata = $data['daily_logs_2___user_raw'];
$query = $dbcon->getQuery(true);
$query
->select('AVG(food_score) AS myavg_foodscore')
->from('daily_logs_2')
->where('user = '. $dbcon->quote($mydata));
$dbcon->setquery($query);
$myavg_foodscore = $dbcon->loadResult();

$query = $dbcon->getQuery(true);
$query
->select('AVG(food_score) AS avg_foodscore')
->from('daily_logs_2')
$dbcon->setquery($query);
$avg_foodscore = $dbcon->loadResult();
 
If I block out the second query, then there is no error. Otherwise, I continue to get an error -
Here is what I ran..
$dbcon = JFactory::getDbo();
$foodscores = $data['daily_logs_2___food_score'];
$mydata = $data['daily_logs_2___user_raw'];
$query = $dbcon->getQuery(true);
$query
->select('AVG(food_score) AS myavg_foodscore')
->from('daily_logs_2')
->where('user = '. $dbcon->quote($mydata));
$dbcon->setquery($query);
$myavg_foodscore = $dbcon->loadResult();

//$query = $dbcon->getQuery(true);
//$query
//->select('AVG(food_score) AS avg_foodscore')
//->from('daily_logs_2')
//$dbcon->setquery($query);
//$avg_foodscore = $dbcon->loadResult();

If I remove the // from the above lines, then I get the error. With the next query blocked, it runs ok.
 
Syntax error: in the second query there's a semicolon missing at the end of the line
Code:
->from('daily_logs_2')
Thank you! That works.

The list displays ok. However, the form shows an error - "1054 Unknown column 'Array' in 'where clause'"
 
Last edited:
However, the form shows an error - "1054 Unknown column 'Array' in 'where clause'"
You didn't say where/how you're using the code here above, so it's hard to say if it could be the cause... though likely not if the list is fine.
So, since it may be caused by your code here above, or not but then by something else, like DBjoin, form plugin or so: use max error display, debug (to see the queries), eventually disable things one by one to find the culprit.
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top