1. Fabrik 3.9 has been released. If you have updated Joomla to 3.9, this is a required update.
    Dismiss Notice

Can I do multiple queries?

Discussion in 'Community' started by madpad, Aug 14, 2020.

  1. madpad

    madpad Member

    Level: Community
    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.
     
  2. juuser

    juuser Active Member

    Level: Community
    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.
     
    madpad likes this.
  3. startpoint

    startpoint Active Member

    Level: Community
    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();
     
    madpad likes this.
  4. madpad

    madpad Member

    Level: Community
    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.
     
  5. lousyfool

    lousyfool Active Member

    Level: Community
    Syntax error: in the second query there's a semicolon missing at the end of the line
    Code (Text):
    ->from('daily_logs_2')
     
    juuser and madpad like this.
  6. madpad

    madpad Member

    Level: Community
    Thank you! That works.

    The list displays ok. However, the form shows an error - "1054 Unknown column 'Array' in 'where clause'"
     
    Last edited: Aug 15, 2020
  7. lousyfool

    lousyfool Active Member

    Level: Community
    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.
     

Share This Page