Documentation

Query

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

The method sends database query and if there is no error, returns the query result. Returns CDBResult class object on success.
Specifies to ignore errors. If set to "true", the method returns "false" when an error is encountered.
If error occurred and the ignore_errors parameter equals "false", the method interrupts page execution and performs the following operations beforehand:

  1. Calls the AddMessage2Log method.
  2. If the current user is a site administrator, the /bitrix/php_interface/dbconn.php file has the initialized $DBDebug=true; variable, the screen will display the full text of the error, otherwise the SendError method will be called.
  3. The /bitrix/php_interface/dbquery_error.php file will be connected. If it doesn't exist, the /bitrix/modules/main/include/dbquery_error.php file will be connected instead.

Notes for Oracle version:
1. When an error occurs and transaction was opened, the method CDataBase::Rollback is called.
2. Use the CDatabase::QueryBind method to ins ert text fields of BLOB, CLOB, LONG type and etc. (if their length is more than 4000 symbols).
3. When the "SEL ECT" type SQL query is executed, binding of variables is required, use the method CDatabase::QueryBindSelect.

Non-static method.

Parameters

ParameterDescription Available fr om version
sql SQL query.
ignore_errors Ignore errors. If true, then fucntion returns "false" on error. If the parameter ignore_errors equals "false", then in case of an error the function terminates execution of complete page.
Optional; "false" by default.
error_position String that identifies position in code from where this CDatabase::Query method was called. If an SQL query fails and the file/bitrix/php_interface/dbconn.php has the $DBDebug=true; variable, both error_position and SQL query will be displayed on the screen. Optional.
Options Optional.

See Also

Examples of use

<?
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 = "
		SELE CT
			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
		FR OM 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')
		WH ERE 
			$where
		GROUP BY 
			F.ID
		";
	$res = $DB->Query($strSql, false, $err_mess.__LINE__);
	return $res;
}
?>


© «Bitrix24», 2001-2024
Up