Using Zend_Db Adapter method we use the following approach to connect database and execute SQL statement.
//Connect to database
$db = Zend_Db::factory(‘DB_ADAPTER_TYPE’, array(
‘host’ => ‘127.0.0.1’,
‘username’ => ‘webuser’,
‘password’ => ‘xxxxxxxx’,
‘dbname’ => ‘test’,
‘adapterNamespace’ => ‘MyProject_Db_Adapter’
//To execute the query
But in the above method, when the query length exceeds 4000 characters , it dies silently without producing any error. (Note that sometime we need to insert huge xml file ,text file or html file content to database. In that case the query becomes ver large). This type of issue normally comes in windows and mac servers. This happens due to the preg methods. Normally PHP preg methods unable to process large data because of stack size. Since zend_db is heavily dependent on the preg methods , it produces the same issue when large query is executed using zend_db adapter methods.
Normally when query is executed using zend_db adapter methods ($db->query(…);) it performs 2 types of tasks. Such as prepares the statement and then execute the statement. During prepare statement it uses most of the preg methods.
To overcome the above issue we can use the below approaches.
If ‘DB_ADAPTER_TYPE’ is ‘Pdo_Mysql’ or ‘Pdo_Mssql’, then we can use the below syntax for execution of large query.
$result = $db->getConnection()->query($sqlquery);
If ‘Pdo_Mssql’ is not available and we need to connect to SqlServer database, then we can use DB_ADAPTER_TYPE as ‘Sqlsrv’. But in this case we can’t use the above method to get rid of large query issue. So here we can use the below line of code to fix our large query issue.
$result = sqlsrv_query( $db->getConnection() , $sqlquery );
Here ‘sqlsrv_query()’ is a msdn library method and can be executable by PHP.