sqlsrv_next_result
(Информация о версии неизвестна, возможно, только в SVN)
sqlsrv_next_result — Makes the next result of the specified statement active
Описание
Makes the next result of the specified statement active. Results include result sets, row counts, and output parameters.
Список параметров
-
stmt
-
The statment on which the next result is being called.
Возвращаемые значения
Returns TRUE
if the next result was successfully retrieved, FALSE
if an error
occurred, and NULL
if there are no more results to retrieve.
Примеры
Пример #1 sqlsrv_next_result() example
The following example executes a batch query that inserts into a table and then selects from the table. This produces two results on the statement: one for the rows affected by the INSERT and one for the rows returned by the SELECT. To get to the rows returned by the SELECT, sqlsrv_next_result() must be called to move past the first result.
<?php
$serverName = "serverName\sqlexpress";
$connectionInfo = array("Database"=>"dbName", "UID"=>"userName", "PWD"=>"password");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
$query = "INSERT INTO Table_1 (id, data) VALUES (?,?); SELECT * FROM TABLE_1;";
$params = array(1, "some data");
$stmt = sqlsrv_query($conn, $query, $params);
// Consume the first result (rows affected by INSERT) without calling sqlsrv_next_result.
echo "Rows affected: ".sqlsrv_rows_affected($stmt)."<br />";
// Move to the next result and display results.
$next_result = sqlsrv_next_result($stmt);
if( $next_result ) {
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC)){
echo $row['id'].": ".$row['data']."<br />";
}
} elseif( is_null($next_result)) {
echo "No more results.<br />";
} else {
die(print_r(sqlsrv_errors(), true));
}
?>
Смотрите также
- sqlsrv_query() - Prepares and executes a query.
- sqlsrv_fetch_array() - Returns a row as an array
- sqlsrv_rows_affected() - Returns the number of rows modified by the last INSERT, UPDATE, or DELETE query executed
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Расширения для работы с базами данных
- Расширения для работы с базами данных отдельных производителей
- Microsoft SQL Server Driver for PHP
- sqlsrv_begin_transaction
- sqlsrv_cancel
- sqlsrv_client_info
- sqlsrv_close
- sqlsrv_commit
- sqlsrv_configure
- sqlsrv_connect
- sqlsrv_errors
- sqlsrv_execute
- sqlsrv_fetch_array
- sqlsrv_fetch_object
- sqlsrv_fetch
- sqlsrv_field_metadata
- sqlsrv_free_stmt
- sqlsrv_get_config
- sqlsrv_get_field
- sqlsrv_has_rows
- sqlsrv_next_result
- sqlsrv_num_fields
- sqlsrv_num_rows
- sqlsrv_prepare
- sqlsrv_query
- sqlsrv_rollback
- sqlsrv_rows_affected
- sqlsrv_send_stream_data
- sqlsrv_server_info
Коментарии
You can use a for loop to iterate through the results:
If you know how many results there should be it's easier, otherwise you can put a large number in the loop constructor and break when done:
<?
for ($i=0; $i< (Large # or Exact #); $i++)
{
$rows_affected = sqlsrv_rows_affected($q2);
if ($rows_affected === false)
{
echo 'Inserts Failed<br />\n';
} else if ( $rows_affected == -1) {
echo "No information available.<br />\n";
} else {
echo $rows_affected." rows were added.<br />\n";
}
$next_result = sqlsrv_next_result($q2);
if (!$next_result)
{
break;
}
}
?>