2

I have the following code:

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

So far it works, but when I add a bit to it so it looks like 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 was longer but I stripped it down to this trying to isolate the problem.)

Edit: It is not Empty because var_dump($stmt) still says resource(9, SQL Server Statement) but the while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)){*more code*} in the table creation section won't even run once.

4 Answers 4

4

It's behaving perfectly well.

Just copying the variable in that context does nothing really, it still points to the same resource, and that resource internally has a row pointer. When you fetch all of its rows the first time, you move that pointer to its last position and that's where it stays.

It should be possible to move the pointer back to the first position, but that probably requires you to make the resource 'Scrollable' via the 4th parameter to sqlsrv_query() (not really sure about that one).

Sign up to request clarification or add additional context in comments.

Comments

3

Why should sqlsrv_fetch($stmtForCounting) not affect $stmt? You're assigning the same resource to $stmt as well as to $stmtForCounting, and by while-looping over the resource, you're traversing the table to its end, which can be done just once.7

If you var_dump both, both will yield the same output (i.e., the same resource).

Perhaps to clarify: You open the resource by executing the query and initialize a variable $stmt which points to this resource. Afterwards, you initialize another variable, which contains a copy of $stmt, which points to the resource. True, both variables are independent, you can e.g. unset one and the other still points to the resource. But the point is, they're pointing to the same resource, hence, when you perform actions on them like fetching rows, the state of the resource is modified (it points to the next row), and this change takes place regardless whether you access the resource via $stmt or via another variable pointing to the same resource.

2 Comments

"same resource" = pointer to result set/current row. copying the statement object just copies that pointer, hence they both point at the same result set, which is depleted the first time around. - Ah, the comment has been deleted, so now the rest of us are talking to ourselves.
Sorry, deleted my first comment, I shouldn't have done it so here it is for anyone who looks at this post in the future: Shouldn't $stmt = $stmtForCounting result in 2 separate statement variables?
1

It will mess up your $stmt definetly, it is same resource, and you try to read all of it twice.

Comments

-3

try to use mssql query instead of sqlsrv query take a look here http://php.net/manual/en/ref.mssql.php

5 Comments

Deprecated. From the Introduction of that same section: This extension is not available anymore on Windows with PHP 5.3 or later. Also, you should at least explain why you recommend this, with 'It's just what I know' not being sufficient. Still, that wouldn't answer the question posed.
I don't think that would solve anything and as you can read here: link mssql is no longer maintained nor supported by Microsoft
I assumed the server was linux, sorry
A very fair possiblity, but in order to keep up with SQL Server advances, a Linux user would have to acquire a compiled copy of sqlsrv anyway. If there are any reasons to prefer (continuing) using mssql, I'd be interested!
(I think sqlsrv is available for linux as of 2012) damn, always late with comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.