sqlsrv UPDATE statement runs but doesn't updat

2019-07-30 13:44发布

问题:

I have an issue where my PHP sqlsrv UPDATE statement runs and doesn't return any errors but nothing in the DB ever changes and the row is not actually updated. Is there something I am missing that would allow this query to appear to run successfully but not update the row?

I have checked that the update statement runs in SQL server management studio and it updates the row.

$visitquery = "UPDATE tblVisit SET CalledInBy='WINNER' WHERE VisitID='3679061'";            

                    $visitClose = sqlsrv_prepare($connect,$visitquery);
                    if( sqlsrv_execute( $visitClose))
                        {
                              echo "Statement executed.\n <br />";
                        }
                        else
                        {
                             echo "Error in executing statement.\n";
                             die( print_r( sqlsrv_errors(), true));
                        } 

回答1:

Thanks @JonathanAmend

sqlsrv_begin_transaction was opened but wasn't calling sqlsrv_commit.

http://www.php.net/manual/en/function.sqlsrv-commit.php



回答2:

Make sure that your script has permission to access the correct database or nothing will happen to the tables.

So first, establish a connection to the database then specify the database that contains your table.

In your setup, add this line above your code.

$connect = sqlsrv_connect(server format goes here,array("Database"=>name of database containing tblVisit in quotes goes here,"UID"=>username to access database goes here,"PWD"=>password to access database goes here));

For each value, enclose them with quotes since they are strings.

There is more information on how to make the code here:

See this (at php.net)