MS SQL Server Concepts and Programming Question:
Download Questions PDF

PHP ODBC - How To Get the Number of Affected Rows?

MS SQL Server Interview Question
MS SQL Server Interview Question

Answer:

If you insert multiple rows with a single INSERT statement, you can use the odbc_num_rows() function to find out how many rows were inserted. odbc_num_rows($result_set) returns the number of affected rows based on the result set object returned by the last INSET, UPDATE or DELETE statement.

The following tutorial script shows you report back the number of rows inserted properly:

<?php
$con = odbc_connect('ggl_SQL_SERVER','sa','GlobalGuideLine');

$sql = "INSERT INTO ggl_links"
. " SELECT id+1000, REVERSE(url), notes, counts, time"
. " FROM ggl_links WHERE id > 1000";
$res = odbc_exec($con, $sql);
if (!$res) {
print("SQL statement failed with error: ");
print(odbc_error($con).": ".odbc_errormsg($con)." ");
} else {
$number_of_rows = odbc_num_rows($res);
print("$number_of_rows rows inserted. ");
}

odbc_close($con);

If you run this script, you should get:

2 rows inserted


Download MS SQL Server Interview Questions And Answers PDF

Previous QuestionNext Question
PHP ODBC - How To Insert Multiple Rows with a subquery?PHP ODBC - What Is a Result Set Object Returned by odbc_exec()?