Bitrix Site Manager

CDatabase::Query

mixed
CDatabase::Query(
 string sql,
 bool ignore_errors=false,
 string error_position="" 
)

The method Query performs a query to the database and returns the query result.

Parameters

ParameterDescription
sql SQL query.
ignore_errors Specifies to ignore errors. If set to true, the method returns false on failure. If set to false, the method breaks the page execution.
Optional; false by default.
error_position String indicating the method call position in the code (e.g. __LINE__). If an SQL query fails and the file /bitrix/php_interface/dbconn.php has the variable $DBDebug=true;, the error_position and the failure SQL query are printed.

Return Values

The method returns an instance of CDBResult on success.

If an error occurs and the ignore_errors parameter is true, the function returns false. If the ignore_errors parameter is false, the method breaks the page execution, having the following operations performed beforehand.

  1. Calls the function AddMessage2Log.
  2. If the current user is an administrator or the file /bitrix/php_interface/dbconn.php has the variable $DBDebug=true; the full error description is printed. Otherwise, the function SendError is called.
  3. The file /bitrix/php_interface/dbquery_error.php is included. If this file does not exist, the file /bitrix/modules/main/include/dbquery_error.php is included instead.

Notes for Oracle version:
1. In case an error occurred and a transaction is open, the metod CDataBase::Rollback is called.
2. Use the method CDatabase::QueryBind to insert the text fields BLOB, CLOB, LONG etc. (if their length is more than 4000 symbols).
3. If the an SQL query (like "SELECT") requires variable binding, use the method CDatabase::QueryBindSelect.

See Also

Example



<?
function GetByID($ID, $GET_BY_SID="N")
{
    $err_mess = (CForm::err_mess())."<br>Function: GetByID<br>Line: ";
    global $DB;
    $where = ($GET_BY_SID=="N") ? " F.ID = '".intval($ID)."' " : " F.VARNAME='".$DB->ForSql($ID,50)."' ";
    $strSql = "
        SELECT
            F.*,
            F.FIRST_SITE_ID,
            F.FIRST_SITE_ID             LID,
            F.VARNAME,
            F.VARNAME                   SID,
            ".$DB->DateToCharFunction("F.TIMESTAMP_X")." TIMESTAMP_X,
            count(distinct D1.ID)       C_FIELDS,
            count(distinct D2.ID)       QUESTIONS,
            count(distinct S.ID)        STATUSES
        FROM b_form F
        LEFT JOIN b_form_status S ON (S.FORM_ID = F.ID)
        LEFT JOIN b_form_field D1 ON (D1.FORM_ID = F.ID and D1.ADDITIONAL='Y')
        LEFT JOIN b_form_field D2 ON (D2.FORM_ID = F.ID and D2.ADDITIONAL<>'Y')
        WHERE 
            $where
        GROUP BY 
            F.ID
        ";
    $res = $DB->Query($strSql, false, $err_mess.__LINE__);
    return $res;
}
?>