Skip to main content
Improved suggested snippet
Source Link
mickmackusa
  • 4.8k
  • 4
  • 18
  • 48
$result = [1, 2];  // fake data
$db = JFactory::getDBO();
try {
    $query = $db->getQuery(true)
                ->select('FieldValue')
                ->from('#__rsform_submission_values')
                ->where(
                    array([
                        "FieldName IN ('Container1','Container2','Container3',Container4')",
                        "SubmissionID IN (" . implode(',', $result) . ")"
                    )
                ]);
    echo $query->dump();                       // never show this to the public
    $resultset = $db->execute>loadObjectList();
    if (!$resultset) {
        echo "No Rows In Resultset";Result Set";
    } else {
        var_export($db->loadObjectList$resultset);
    }
} catch (Exception $e) {
    echo  "Syntax Error: " , $e->getMessage(); // never show this to the public
}
$result = [1, 2];  // fake data
$db = JFactory::getDBO();
try {
    $query = $db->getQuery(true)
                ->select('FieldValue')
                ->from('#__rsform_submission_values')
                ->where(
                    array(
                        "FieldName IN ('Container1','Container2','Container3',Container4')",
                        "SubmissionID IN (" . implode(',', $result) . ")"
                    )
                );
    echo $query->dump();                       // never show this to the public
    $resultset = $db->execute();
    if (!$resultset) {
        echo "No Rows In Resultset";
    } else {
        var_export($db->loadObjectList);
    }
} catch (Exception $e) {
    echo  "Syntax Error: " , $e->getMessage(); // never show this to the public
}
$result = [1, 2];  // fake data
$db = JFactory::getDBO();
try {
    $query = $db->getQuery(true)
        ->select('FieldValue')
        ->from('#__rsform_submission_values')
        ->where([
            "FieldName IN ('Container1','Container2','Container3',Container4')",
            "SubmissionID IN (" . implode(',', $result) . ")"
          ]);
    echo $query->dump();                       // never show this to the public
    $resultset = $db->loadObjectList();
    if (!$resultset) {
        echo "No Rows In Result Set";
    } else {
        var_export($resultset);
    }
} catch (Exception $e) {
    echo  "Syntax Error: " , $e->getMessage(); // never show this to the public
}
Source Link
mickmackusa
  • 4.8k
  • 4
  • 18
  • 48

Let's not only solve your issue, but teach you how you can solve your own querying issues in the future. After all, if we need to fix every Joomla hiccup that you encounter, then you aren't acting very much like a "developer", your work will come to a grinding halt every time a problem arises, and this community will need to volunteer their time to write your code (which I assume that you are being paid/compensated for).

Most importantly, use a try {} catch {} block to help you to debug your query. If you would have written this:

$db = JFactory::getDBO();
try {
    $db->setQuery("SELECT `SubmissionId`, `FieldValue`, FROM `jos_rsform_submission_values` WHERE `FieldName` IN (\"Waybill1\", \"Waybill2\", \"Waybill3\", \"Waybill4\", \"Waybill5\") AND `FieldValue` != \"\" AND `FormId` = 28");
    $db->execute();
    var_export($db->loadObjectList);
} catch (Exception $e) {
    echo  "Syntax Error: " , $e->getMessage();  // only display getMessage() where not available to the public
}

You would have been presented with:

Syntax Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM `jos_rsform_submission_values` WHERE `FieldName` IN ("Waybill1", "Waybill2"' at line 1

This indicates that there was a syntax error just before the FROM clause. Then you could have compared your query to any other SELECT query on this site or tutorial or manual online to quickly determine that the comma is to blame.

Based on your follow up comment:

I was wondering if you could please help me with one more piece of code: I would like to implode results, and I will post the code here:

$query->select($db->quoteName(array('FieldValue')));
$query->from($db->quoteName('#__rsform_submission_values'));
$query->where($db->quoteName('FieldName') . ' IN ("Container1","Container2","Container3","Container4")');
$query->where($db->quoteName('SubmissionID') . IN . (''.implode("','", $result).''));
$db->setQuery($query);
  1. You are using table and column quoting methods that are not necessary for security/stability/accuracy. Want to know when they are necessary? Read this.
  2. You are feeding the select() an array when a string will do.
  3. You are wrapping your FieldName values in double quotes when single quotes are preferred / more commonplace.
  4. Your second where() call is treating IN as a constant, which is not right, and you are using too many single quotes on both sides of implode().

Here's what I recommend:

$result = [1, 2];  // fake data
$db = JFactory::getDBO();
try {
    $query = $db->getQuery(true)
                ->select('FieldValue')
                ->from('#__rsform_submission_values')
                ->where(
                    array(
                        "FieldName IN ('Container1','Container2','Container3',Container4')",
                        "SubmissionID IN (" . implode(',', $result) . ")"
                    )
                );
    echo $query->dump();                       // never show this to the public
    $resultset = $db->execute();
    if (!$resultset) {
        echo "No Rows In Resultset";
    } else {
        var_export($db->loadObjectList);
    }
} catch (Exception $e) {
    echo  "Syntax Error: " , $e->getMessage(); // never show this to the public
}

This will display your rendered query:

SELECT FieldValue
FROM jos_rsform_submission_values
WHERE FieldName IN ('Container1','Container2','Container3',Container4') AND SubmissionID IN (1,2)

and either an error message, an indication of a null resultset, or your multidimensional resultset.

Notice that the imploded SubmissionID values are not quote wrapped. I am doing this because it is fair to assume that these values are integers. If these values are not integers or they are from an untrusted source (like a user-submitted form), then please read the advice at the bottom of this answer.