function query($Query_String) { // remove the 'LIMIT' clause from the query $pos = strpos($Query_String, " LIMIT "); if ($pos) { $Query_String = substr($Query_String, 0, $pos); } unset($pos); $this->connect(); # printf("<br />Debug: query = %s<br />\n", $Query_String); # rei@netone.com.br suggested that we use this instead of the odbc_exec(). # He is on NT, connecting to a Unix MySQL server with ODBC. -- KK # $this->Query_ID = odbc_prepare($this->Link_ID,$Query_String); # $this->Query_Ok = odbc_execute($this->Query_ID); $this->Query_ID = odbc_exec($this->Link_ID, $Query_String); $this->Row = 0; odbc_binmode($this->Query_ID, 1); odbc_longreadlen($this->Query_ID, 4096); if (!$this->Query_ID) { $this->Errno = 1; $this->Error = "General Error (The ODBC interface cannot return detailed error messages)."; $this->halt("Invalid SQL: " . $Query_String); } return $this->Query_ID; }
function query($Query_String) { /* No empty queries, please, since PHP4 chokes on them. */ if ($Query_String == "") { /* The empty query string is passed on from the constructor, * when calling the class without a query, e.g. in situations * like these: '$db = new DB_Sql_Subclass;' */ return 0; } $this->connect(); # printf("<br>Debug: query = %s<br>\n", $Query_String); # rei@netone.com.br suggested that we use this instead of the odbc_do(). # He is on NT, connecting to a Unix MySQL server with ODBC. -- KK # $this->Query_ID = odbc_prepare($this->Link_ID,$Query_String); # $this->Query_Ok = odbc_execute($this->Query_ID); $this->Query_ID = odbc_do($this->Link_ID, $Query_String); $this->Row = 0; odbc_binmode($this->Query_ID, 1); odbc_longreadlen($this->Query_ID, 4096); if (!$this->Query_ID) { $this->Errno = 1; $this->Error = "General Error (The ODBC interface cannot return detailed error messages)."; $this->halt("Invalid SQL: " . $Query_String); } return $this->Query_ID; }
/** * Send an SQL query * @param String sql * @return Mixed */ public function query($sql) { $this->debugInfo($sql); $rs = odbc_exec($this->conn, $sql); if (!$rs) { trigger_error(odbc_error(), E_USER_ERROR); return FALSE; } odbc_binmode($rs, ODBC_BINMODE_RETURN); odbc_longreadlen($rs, 1024 * 1024); return new QueryResult($this, $rs); }
function db_query($qstring,$conn) { global $strLastSQL,$dDebug; if ($dDebug===true) echo $qstring."<br>"; $strLastSQL=$qstring; if(!($rs=odbc_exec($conn,$qstring))) trigger_error(odbc_error(), E_USER_ERROR); odbc_binmode($rs,ODBC_BINMODE_RETURN); odbc_longreadlen($rs,1024*1024); return $rs; }
function _query($sql, $inputarr = false) { global $php_errormsg; if (isset($php_errormsg)) { $php_errormsg = ''; } $this->_error = ''; if ($inputarr) { if (is_array($sql)) { $stmtid = $sql[1]; } else { $stmtid = odbc_prepare($this->_connectionID, $sql); if ($stmtid == false) { $this->_errorMsg = isset($php_errormsg) ? $php_errormsg : ''; return false; } } if (!odbc_execute($stmtid, $inputarr)) { //@odbc_free_result($stmtid); if ($this->_haserrorfunctions) { $this->_errorMsg = odbc_errormsg(); $this->_errorCode = odbc_error(); } return false; } } else { if (is_array($sql)) { $stmtid = $sql[1]; if (!odbc_execute($stmtid)) { //@odbc_free_result($stmtid); if ($this->_haserrorfunctions) { $this->_errorMsg = odbc_errormsg(); $this->_errorCode = odbc_error(); } return false; } } else { $stmtid = odbc_exec($this->_connectionID, $sql); } } $this->_lastAffectedRows = 0; if ($stmtid) { if (@odbc_num_fields($stmtid) == 0) { $this->_lastAffectedRows = odbc_num_rows($stmtid); $stmtid = true; } else { $this->_lastAffectedRows = 0; odbc_binmode($stmtid, $this->binmode); odbc_longreadlen($stmtid, $this->maxblobsize); } if ($this->_haserrorfunctions) { $this->_errorMsg = ''; $this->_errorCode = 0; } else { $this->_errorMsg = isset($php_errormsg) ? $php_errormsg : ''; } } else { if ($this->_haserrorfunctions) { $this->_errorMsg = odbc_errormsg(); $this->_errorCode = odbc_error(); } else { $this->_errorMsg = isset($php_errormsg) ? $php_errormsg : ''; } } return $stmtid; }
/** * Get file contents * * @param string $path file path * @return string|false * @author Dmitry (dio) Levashov **/ protected function _getContents($path) { $sql = "SET TEXTSIZE 2147483647 "; // increase mssql or odbc data size limit $sql .= sprintf('SELECT convert(varbinary(max),content) as content FROM %s WHERE id=%d', $this->tbf, $path); $res = $this->query($sql); odbc_binmode($res, ODBC_BINMODE_RETURN); //long binary handling odbc_longreadlen($res, 500000000); //increase mssql or odbc data size 500 Megabytes $r = odbc_fetch_array($res); return $res && $r ? base64_decode($r['content']) : false; }
/** * Reads in any unread LOB data. For long char fields, we may already * have up to odbc_longreadlen() bytes in the buffer. These are passed * in via the $curdata parm. For long binary fields, no data is read * initially since odbc_binmode() is set to ODBC_BINMODE_PASSTHRU. * This method adjusts the binmode and longreadlen to finish reading * these datatypes into the buffer. Returns a string with the complete * contents. * * @param int|string $column Column index or name to read data from. * @param int $binmode ODBC_BINMODE_RETURN for binary data, ODBC_BINMODE_CONVERT for char data. * @param string $curdata Existing LOB data already in buffer. * @return string */ protected function readLobData($column, $binmode, $curdata = null) { // Retrieve field num $fldNum = is_int($column) ? $column : getFieldNum($column); $data = $curdata; $newdata = null; // Adjust binmode and longreadlen odbc_binmode($this->result->getHandle(), $binmode); odbc_longreadlen($this->result->getHandle(), 4096); while (1) { $newdata = odbc_result($this->result->getHandle(), $fldNum); if ($newdata === false) { break; } else { $data .= $newdata; } } // Restore the default binmode and longreadlen odbc_binmode($this->result->getHandle(), ODBC_BINMODE_PASSTHRU); odbc_longreadlen($this->result->getHandle(), ini_get('odbc.defaultlrl')); // The ODBC driver I use seems to return a string with an escaped // null char at the end for clob data. $data = rtrim($data, ""); return $data; }
/** * Executes an SQL query * * @param fResult $result The result object for the query * @return void */ private function executeQuery($result) { // We don't want errors and an exception $old_level = error_reporting(error_reporting() & ~E_WARNING); if ($this->extension == 'mssql') { $result->setResult(mssql_query($result->getSQL(), $this->connection)); } elseif ($this->extension == 'mysql') { $result->setResult(mysql_query($result->getSQL(), $this->connection)); } elseif ($this->extension == 'mysqli') { $result->setResult(mysqli_query($this->connection, $result->getSQL())); } elseif ($this->extension == 'oci8') { $oci_statement = oci_parse($this->connection, $result->getSQL()); if (oci_execute($oci_statement, $this->inside_transaction ? OCI_DEFAULT : OCI_COMMIT_ON_SUCCESS)) { oci_fetch_all($oci_statement, $rows, 0, -1, OCI_FETCHSTATEMENT_BY_ROW + OCI_ASSOC); $result->setResult($rows); unset($rows); } else { $result->setResult(FALSE); } } elseif ($this->extension == 'odbc') { $resource = odbc_exec($this->connection, $result->getSQL()); if (is_resource($resource)) { $rows = array(); // Allow up to 1MB of binary data odbc_longreadlen($resource, 1048576); odbc_binmode($resource, ODBC_BINMODE_CONVERT); while ($row = odbc_fetch_array($resource)) { $rows[] = $row; } $result->setResult($rows); unset($rows); } else { $result->setResult($resource); } } elseif ($this->extension == 'pgsql') { $result->setResult(pg_query($this->connection, $result->getSQL())); } elseif ($this->extension == 'sqlite') { $result->setResult(sqlite_query($this->connection, $result->getSQL(), SQLITE_ASSOC, $sqlite_error_message)); } elseif ($this->extension == 'sqlsrv') { $resource = sqlsrv_query($this->connection, $result->getSQL()); if (is_resource($resource)) { $rows = array(); while ($row = sqlsrv_fetch_array($resource, SQLSRV_FETCH_ASSOC)) { $rows[] = $row; } $result->setResult($rows); unset($rows); } else { $result->setResult($resource); } } elseif ($this->extension == 'pdo') { if (preg_match('#^\\s*CREATE(\\s+OR\\s+REPLACE)?\\s+TRIGGER#i', $result->getSQL())) { $this->connection->exec($result->getSQL()); $pdo_statement = FALSE; $returned_rows = array(); } else { $pdo_statement = $this->connection->query($result->getSQL()); $returned_rows = is_object($pdo_statement) ? $pdo_statement->fetchAll(PDO::FETCH_ASSOC) : $pdo_statement; // The pdo_pgsql driver likes to return empty rows equal to the number of affected rows for insert and deletes if ($this->type == 'postgresql' && $returned_rows && $returned_rows[0] == array()) { $returned_rows = array(); } } $result->setResult($returned_rows); } error_reporting($old_level); if ($this->extension == 'sqlite') { $this->checkForError($result, $sqlite_error_message); } elseif ($this->extension == 'oci8') { $this->checkForError($result, $oci_statement); } else { $this->checkForError($result); } if ($this->extension == 'pdo') { $this->setAffectedRows($result, $pdo_statement); if ($pdo_statement) { $pdo_statement->closeCursor(); } unset($pdo_statement); } elseif ($this->extension == 'oci8') { $this->setAffectedRows($result, $oci_statement); oci_free_statement($oci_statement); } elseif ($this->extension == 'odbc') { $this->setAffectedRows($result, $resource); odbc_free_result($resource); } elseif ($this->extension == 'sqlsrv') { $this->setAffectedRows($result, $resource); sqlsrv_free_stmt($resource); } else { $this->setAffectedRows($result); } $this->setReturnedRows($result); $this->handleAutoIncrementedValue($result); }
function _query($sql, $inputarr = false) { global $php_errormsg; $php_errormsg = ''; $this->_error = ''; if ($inputarr) { if (is_resource($sql)) { $stmtid = $sql; } else { $stmtid = odbc_prepare($this->_connectionID, $sql); } if ($stmtid == false) { $this->_errorMsg = $php_errormsg; return false; } //print_r($inputarr); if (!odbc_execute($stmtid, $inputarr)) { @odbc_free_result($stmtid); return false; } } else { $stmtid = odbc_exec($this->_connectionID, $sql); } if ($stmtid) { odbc_binmode($stmtid, $this->binmode); odbc_longreadlen($stmtid, $this->maxblobsize); } $this->_errorMsg = $php_errormsg; return $stmtid; }
/** * Executes an unbuffered SQL query * * @param string|fStatement $statement The statement to perform * @param fUnbufferedResult $result The result object for the query * @param array $params The parameters for prepared statements * @return void */ private function performUnbufferedQuery($statement, $result, $params) { $this->setErrorHandler(); $extra = NULL; if (is_object($statement)) { $statement->executeUnbufferedQuery($result, $params, $extra, $statement != $this->statement); } elseif ($this->extension == 'ibm_db2') { $result->setResult(db2_exec($this->connection, $statement, array('cursor' => DB2_FORWARD_ONLY))); } elseif ($this->extension == 'mssql') { $result->setResult(mssql_query($result->getSQL(), $this->connection, 20)); } elseif ($this->extension == 'mysql') { $result->setResult(mysql_unbuffered_query($result->getSQL(), $this->connection)); } elseif ($this->extension == 'mysqli') { $result->setResult(mysqli_query($this->connection, $result->getSQL(), MYSQLI_USE_RESULT)); } elseif ($this->extension == 'oci8') { $extra = oci_parse($this->connection, $result->getSQL()); if (oci_execute($extra, $this->inside_transaction ? OCI_DEFAULT : OCI_COMMIT_ON_SUCCESS)) { $result->setResult($extra); } else { $result->setResult(FALSE); } } elseif ($this->extension == 'odbc') { $extra = odbc_exec($this->connection, $result->getSQL()); if ($extra) { odbc_longreadlen($extra, 1048576); odbc_binmode($extra, ODBC_BINMODE_CONVERT); } $result->setResult($extra); } elseif ($this->extension == 'pgsql') { $result->setResult(pg_query($this->connection, $result->getSQL())); } elseif ($this->extension == 'sqlite') { $result->setResult(sqlite_unbuffered_query($this->connection, $result->getSQL(), SQLITE_ASSOC, $extra)); } elseif ($this->extension == 'sqlsrv') { $result->setResult(sqlsrv_query($this->connection, $result->getSQL())); } elseif ($this->extension == 'pdo') { $result->setResult($this->connection->query($result->getSQL())); } $this->statement = $statement; $this->restoreErrorHandler(); $this->checkForError($result, $extra); }
/** * @see Connection::connect() */ public function connect($dsninfo, $flags = 0) { if (!function_exists('odbc_connect')) { throw new SQLException('odbc extension not loaded'); } $adapterclass = isset($dsninfo['adapter']) ? $dsninfo['adapter'] : null; if (!$adapterclass) { $adapterclass = 'ODBCAdapter'; } else { $adapterclass .= 'Adapter'; } Creole::import('creole.drivers.odbc.adapters.' . $adapterclass); $this->adapter = new $adapterclass(); $this->dsn = $dsninfo; $this->flags = $flags; if (!($this->flags & Creole::COMPAT_ASSOC_LOWER) && !$this->adapter->preservesColumnCase()) { trigger_error('Connection created without Creole::COMPAT_ASSOC_LOWER, ' . 'but driver does not support case preservation.', E_USER_WARNING); $this->flags != Creole::COMPAT_ASSOC_LOWER; } $persistent = ($flags & Creole::PERSISTENT) === Creole::PERSISTENT; if ($dsninfo['database']) { $odbcdsn = $dsninfo['database']; } elseif ($dsninfo['hostspec']) { $odbcdsn = $dsninfo['hostspec']; } else { $odbcdsn = 'localhost'; } $user = @$dsninfo['username']; $pw = @$dsninfo['password']; $connect_function = $persistent ? 'odbc_pconnect' : 'odbc_connect'; $conn = @$connect_function($odbcdsn, $user, $pw, SQL_CUR_USE_IF_NEEDED); if (!is_resource($conn)) { throw new SQLException('connect failed', $this->nativeError(), $odbcdsn); } $this->dblink = $conn; /** * This prevents blob fields from being fetched when a row is loaded * from a recordset. Clob fields however are loaded with up to * 'odbc.defaultlrl' data. This should be the default anyway, but we'll * set it here just to keep things consistent. */ @odbc_binmode(0, ODBC_BINMODE_PASSTHRU); @odbc_longreadlen(0, ini_get('odbc.defaultlrl')); }
<?php if ($conn = odbc_connect($dsn, $dbuser, $dbpwd)) { if ($res = odbc_do($conn, "select gif from php_test where id='{$id}'")) { odbc_binmode($res, 0); odbc_longreadlen($res, 0); if (odbc_fetch_row($res)) { header("content-type: image/gif"); odbc_result($res, 1); exit; } else { echo "Error in odbc_fetch_row"; } } else { echo "Error in odbc_do"; } } else { echo "Error in odbc_connect"; }
/** * Executes the statement in unbuffered mode (if possible) * * @internal * * @param fUnbufferedResult $result The object to place the result into * @param array $params The parameters for the statement * @param mixed &$extra A variable to place extra information needed by some database extensions * @param boolean $different If this statement is different than the last statement run on the fDatabase instance * @return void */ public function executeUnbufferedQuery($result, $params, &$extra, $different) { if ($different && $this->used) { $this->regenerateStatement(); } $this->used = TRUE; $extension = $this->database->getExtension(); $connection = $this->database->getConnection(); $statement = $this->statement; $params = $this->prepareParams($params); // For the extensions that require the statement be passed to the result // object, we store it in a stdClass object so the result object knows // not to free it when done $statement_holder = new stdClass(); $statement_holder->statement = NULL; switch ($extension) { case 'ibm_db2': $extra = $statement; if (db2_execute($statement, $params)) { $statement_holder->statement = $statement; } else { $result->setResult(FALSE); } break; case 'mssql': $result->setResult(mssql_query($result->getSQL(), $this->connection, 20)); break; case 'mysql': $result->setResult(mysql_unbuffered_query($result->getSQL(), $this->connection)); break; case 'mysqli': $extra = $this->statement; if ($statement->execute()) { $statement_holder->statement = $statement; } else { $result->setResult(FALSE); } break; case 'oci8': $result->setResult(oci_execute($statement, $this->database->isInsideTransaction() ? OCI_DEFAULT : OCI_COMMIT_ON_SUCCESS)); break; case 'odbc': $extra = odbc_execute($statement, $params); if ($extra) { odbc_longreadlen($statement, 1048576); odbc_binmode($statement, ODBC_BINMODE_CONVERT); $statement_holder->statement = $statement; } else { $result->setResult($extra); } break; case 'pgsql': $result->setResult(pg_execute($connection, $this->identifier, $params)); break; case 'sqlite': $result->setResult(sqlite_unbuffered_query($connection, $this->database->escape($statement, $params), SQLITE_ASSOC, $extra)); break; case 'sqlsrv': $extra = sqlsrv_execute($statement); if ($extra) { $statement_holder->statement = $statement; } else { $result->setResult($extra); } break; case 'pdo': $extra = $statement->execute(); if ($extra) { $result->setResult($statement); } else { $result->setResult($extra); } break; } if ($statement_holder->statement) { $result->setResult($statement_holder); } return $result; }
function _query($sql, $inputarr = false) { global $php_errormsg; if (isset($php_errormsg)) { $php_errormsg = ''; } $this->_error = ''; if ($inputarr) { if (is_array($sql)) { $stmtid = $sql[1]; } else { $stmtid = odbc_prepare($this->_connectionID, $sql); if ($stmtid == false) { $this->_errorMsg = isset($php_errormsg) ? $php_errormsg : ''; return false; } } if (!odbc_execute($stmtid, $inputarr)) { //@odbc_free_result($stmtid); if ($this->_haserrorfunctions) { $this->_errorMsg = odbc_errormsg(); $this->_errorCode = odbc_error(); } if ($this->_errorCode == '00000') { // MS SQL Server sometimes returns this in combination with the FreeTDS $this->_errorMsg = ''; // driver and UnixODBC under Linux. This fixes the bogus "error" $this->_errorCode = 0; // <*****@*****.**> return true; } return false; } } else { if (is_array($sql)) { $stmtid = $sql[1]; if (!odbc_execute($stmtid)) { //@odbc_free_result($stmtid); if ($this->_haserrorfunctions) { $this->_errorMsg = odbc_errormsg(); $this->_errorCode = odbc_error(); } if ($this->_errorCode == '00000') { // MS SQL Server sometimes returns this in combination with the FreeTDS $this->_errorMsg = ''; // driver and UnixODBC under Linux. This fixes the bogus "error" $this->_errorCode = 0; // <*****@*****.**> return true; } return false; } } else { $stmtid = odbc_exec($this->_connectionID, $sql); } } $this->_lastAffectedRows = 0; if ($stmtid) { if (@odbc_num_fields($stmtid) == 0) { $this->_lastAffectedRows = odbc_num_rows($stmtid); $stmtid = true; } else { $this->_lastAffectedRows = 0; odbc_binmode($stmtid, $this->binmode); odbc_longreadlen($stmtid, $this->maxblobsize); } if ($this->_haserrorfunctions) { $this->_errorMsg = ''; $this->_errorCode = 0; } else { $this->_errorMsg = isset($php_errormsg) ? $php_errormsg : ''; } } else { if ($this->_haserrorfunctions) { $this->_errorMsg = odbc_errormsg(); $this->_errorCode = odbc_error(); } else { $this->_errorMsg = isset($php_errormsg) ? $php_errormsg : ''; } } return $stmtid; }
function Query($strSql, $bIgnoreErrors = false, $error_position = "") { $this->DoConnect(); $this->db_Error = ""; if ($this->DebugToFile || $this->ShowSqlStat) { list($usec, $sec) = explode(" ", microtime()); $start_time = (double) $usec + (double) $sec; } $result = @odbc_exec($this->db_Conn, $strSql); if ($this->DebugToFile || $this->ShowSqlStat) { list($usec, $sec) = explode(" ", microtime()); $end_time = (double) $usec + (double) $sec; $exec_time = round($end_time - $start_time, 10); if ($this->ShowSqlStat) { $this->cntQuery++; $this->timeQuery += $exec_time; $this->arQueryDebug[] = array("QUERY" => $strSql, "TIME" => $exec_time, "TRACE" => function_exists("debug_backtrace") ? debug_backtrace() : false); } if ($this->DebugToFile) { $fp = fopen($_SERVER["DOCUMENT_ROOT"] . "/mssql_debug.sql", "ab+"); $str = "TIME: " . $exec_time . " SESSION: " . session_id() . " \n"; $str .= $strSql . "\n\n"; $str .= "----------------------------------------------------\n\n"; fputs($fp, $str); @fclose($fp); } } if (!$result) { $this->db_Error = "#" . odbc_error() . " " . odbc_errormsg(); if (!$bIgnoreErrors) { AddMessage2Log($error_position . " MSSQL Query Error: " . $strSql . " [" . $this->db_Error . "]", "main"); if ($this->DebugToFile) { $fp = fopen($_SERVER["DOCUMENT_ROOT"] . "/mssql_debug.sql", "ab+"); fputs($fp, "SESSION: " . session_id() . " ERROR: " . $this->db_Error . "\n\n----------------------------------------------------\n\n"); @fclose($fp); } if ($this->debug || @session_start() && $_SESSION["SESS_AUTH"]["ADMIN"]) { echo $error_position . "<br>MSSQL Query Error:<br><font color=#ff0000><pre>" . htmlspecialchars($strSql) . "</pre></font><br>" . $this->db_Error . "<br>"; } else { $error_position = eregi_replace("<br>", "\n", $error_position); SendError($error_position . "\nMSSQL Query Error:\n" . $strSql . " \n [" . $this->db_Error . "]\n---------------\n\n"); } if (file_exists($_SERVER["DOCUMENT_ROOT"] . "/bitrix/php_interface/dbquery_error.php")) { include $_SERVER["DOCUMENT_ROOT"] . "/bitrix/php_interface/dbquery_error.php"; } elseif (file_exists($_SERVER["DOCUMENT_ROOT"] . "/bitrix/modules/main/include/dbquery_error.php")) { include $_SERVER["DOCUMENT_ROOT"] . "/bitrix/modules/main/include/dbquery_error.php"; } else { die("MSSQL Query Error!"); } die; } return false; } if (defined("BX_MSSQL_BINMODE") && BX_MSSQL_BINMODE == true) { @odbc_binmode($result, ODBC_BINMODE_PASSTHRU); @odbc_longreadlen($result, 9999999); } $res = new CDBResult($result); $res->num_rows_affected = intval(odbc_num_rows($result)); return $res; }