开发者

What is the return value after a successful INSERT using sqlsrv_query() in PHP, MSSQL SERVER 2005?

Using the following simple code:

$aQuery = "INSERT into myTable (a, b, c) VALUES (1, 'Hello', 'Goodbye')";
$result = sqlsrv_query($myConn, $aQuery ); //where $myCon开发者_开发知识库n is valid and defined elsewhere
if (!$result) {
    echo 'FAIL';
}
else {
    echo 'It worked BABY!';
}

What is the value of $result if the query executes successfully, and how do I access the resource properly? echo $result; will print: Resource id #8.


When the query fails $result == false

So in your code:

if (!$result) { //if result is NOT true
    echo 'FAIL'; //it failed
}
else { //otherwise
    echo 'It worked BABY!'; //it did NOT fail
}

Any value that is not false or 0 is considered true


Addendum based on comments below.

Only SELECT returns content.


I think the technical term for $result is a PHP "Statement Resource" (see: http://msdn.microsoft.com/en-us/library/cc793139(v=sql.90).aspx) which is a fancy name for "whatever the database returns".

So $result will contain whatever SQL Server returns for insert statements. I think it is a number of the affected records in the query, but I'm not certain. If you wanted to return something different, say the primary key of the record you just inserted, you would probably need to call a stored procedure rather than just send an insert statement.


Example from Geeks With Blogs

while($row = sqlsrv_fetch_array($result))
{
    echo($row['ID'] . ', '.
     $row['Title'] . ', '.
     $row['Name']);
}

Where ID, Title and Name are columns in the table

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜