sql server - PHP mssql behaving weird with different statements -


i have following code:

$stmt = sqlsrv_query($conn, $sql, $params); $stmtforcounting = $stmt; while ($row = sqlsrv_fetch_array($stmt, sqlsrv_fetch_assoc)){ *table creation code* } 

so far works, when add bit looks this:

$stmt = sqlsrv_query($conn, $sql, $params); $stmtforcounting = $stmt; while (sqlsrv_fetch($stmtforcounting)){ } while ($row = sqlsrv_fetch_array($stmt, sqlsrv_fetch_assoc)){ *table creation code* } 

the $stmt variable becomes empty. using sqlsrv_fetch($stmtforcounting) shouldn't affect $stmt, right?

(the original code longer stripped down trying isolate problem.)

edit: not empty because var_dump($stmt) still says resource(9, sql server statement) while ($row = sqlsrv_fetch_array($stmt, sqlsrv_fetch_assoc)){*more code*} in table creation section won't run once.

why should sqlsrv_fetch($stmtforcounting) not affect $stmt? you're assigning same resource $stmt $stmtforcounting, , while-looping on resource, you're traversing table end, can done once.7

if var_dump both, both yield same output (i.e., same resource).

perhaps clarify: open resource executing query , initialize variable $stmt points resource. afterwards, initialize variable, contains copy of $stmt, points resource. true, both variables independent, can e.g. unset 1 , other still points resource. point is, they're pointing same resource, hence, when perform actions on them fetching rows, state of resource modified (it points next row), , change takes place regardless whether access resource via $stmt or via variable pointing same resource.


Comments