Quickly put all data from SQL query into array using PHP
I am using something like the following PHP code to put all data from a request into an array:
<?php
$results = array();
$q = odbc_exec("SELECT * FROM table");
while ($row = odbc_fetch_array($q)) {
$results[] = $row;
}
?>
This works fine, but is very slow when the query contains thousands of rows.
My question is, is there any way in PHP to dump all data into an array without having to loop through each entry in turn?
NOTE. Using MS SQL Server for the database.
source to share
Specifying cursor_type
in odbc_connect
significantly speeds up the extraction of odbc.
$conn = odbc_connect($dsn, $user, $pass, SQL_CUR_USE_ODBC)
http://aaronsaray.com/blog/2007/08/02/odbc-for-udb-and-php-how-i-increased-performance-by-400/
source to share
You can try using mssql functions instead of odbc, but that hardly matters.
With the way drivers work, the result set is an iterator descriptor for the result data itself. In some cases php does not actually receive data until the string asks for php. If there is no fetch in the driver, then all loops going through all lines are usually your only option.
If you don't need all the columns, you can restrict the data transfer by specifying only the columns that you really need.
source to share