PHP Unconference Europe 2015

mssql_fetch_batch

(PHP 4 >= 4.0.4, PHP 5, PECL odbtp >= 1.1.1)

mssql_fetch_batchRetourne le prochain lot de lignes MS SQL Server

Description

int mssql_fetch_batch ( resource $result )

Retourne le prochain lot de lignes MS SQL Server.

Liste de paramètres

result

La ressource de résultats à évaluer. Ce résultat provient d'un appel à la fonction mssql_query().

Valeurs de retour

Retourne le nombre de lignes du lot retourné.

Exemples

Exemple #1 Exemple avec mssql_fetch_batch()

<?php
// Connexion à MSSQL et sélection de la base de données
$link mssql_connect('MANGO\SQLEXPRESS''sa''phpfi');
mssql_select_db('php'$link);

// Envoi d'une requête
$result mssql_query('SELECT * FROM [php].[dbo].[people]'$link100);
$records 10;

while (
$records >= 0) {
    while (
$row mssql_fetch_assoc($result))
    {
        
// Traitement
    
}

    --
$records;
}

if (
$batchsize mssql_fetch_batch($result)) {
    
// $batchsize est le nombre de ligne à lire
    // dans le résultat, mais enocre à afficher
}
?>

add a note add a note

User Contributed Notes 4 notes

up
1
salvarez at jetmultimedia dot es
7 months ago
If you are doing more than one mssql_query(), you must do a mssql_free_result() to avoid undesirable results of mssql_fetch_batch()
up
0
jeffreznik
1 year ago
I was having issues doing two different select queries with batch sizes in the same connection.  The first one would run fine, the second one would always return one batch + 1 record (ie. batch size on the 2nd query was 250000 records, it would stop after 250001 and say it was complete even though there should have been 3m+ records).

I was able to overcome this by disconnecting from the DB (via mssql_close($conn);) and reconnecting before I ran the second query.  Now my second query returns all the records I need.

Though I would write this here as there is little to no documentation on this issue, and it may only be affecting me.  I am connecting from Linux (RHEL5) to SQL Server 2005 on the network, using FreeTDS.  If you have this same issue, try the above and it may work for you.
up
0
doug dot pe at gmail dot com
2 years ago
This demonstrates the return value:
<?php
$connect
= mssql_connect("databasename", "username", "password");
$sql = "SELECT * FROM huge_table";    //    returns 3271 rows, for example
$qry = mssql_query($SQL, $connect, 1000); // 1000 Rows batchsize
$batchsize = mssql_num_rows($qry);
print
"$batchsize <br />\r\n";
do {
   
$batchsize = mssql_fetch_batch($qry);
    print
"$batchsize <br />\r\n";
} while (
$batchsize);  // get the next batch until end
?>
returns:
1000
1000
1000
271
0
up
0
brutalex430
4 years ago
This could be usefull if you have a large dataset:

<?php
$qry
= mssql_query("SELECT * FROM huge_table", $conn, 1000); // 1000 Rows batchsize

   
do {
      while (
$row = mssql_fetch_row($qry)) {
         
// do something like
         
print_r($row);
      }
    } while (
mssql_fetch_batch($navqry));  // get the next batch until end

?>
To Top