4400

MSSQL and PHP: “An invalid parameter was passed to sqlsrv_query.”

Question:

I'm trying to run a very simple MSSQL update statement through PHP, but I get an error saying *"An invalid parameter was passed to sqlsrv_query."*

Here's the <a href="http://pastebin.com/3j4zmHJY" rel="nofollow">connection class</a> I'm using, and most relevant is the query function:

function query($query) { $result = sqlsrv_query($query) or die( print_r( sqlsrv_errors(), true)); }

Finally, here's the code I'm using to try to execute the statement:

$db = new mssqlClass(); $conn = $db->connect(); $SQL = "UPDATE table SET Title = 'Test' where ID = 1" $SQL = $db->query($SQL);

If anyone has any suggestions I'd be very grateful, I guess it's a problem with the connection class but I'm unsure. Thank you!

(edit) In trying to edit everything down for the purpose of posting here, I somehow missed the 'where' from my statement, agh! Sorry. Needless to say <strong>the query is fine, and when copied into SQL Server Studio Manager executes without any issues</strong>. Cheers

Answer1:

I don't like answering my own questions, but since I made such a mess of asking this question initially I'm hoping this is helpful for anyone seeing this from Google.

Unlike mssql_query, the newer sqlsrv_query function needs the connection paramter to be passed in as the first variable, like so:

$result = sqlsrv_query($this->connection, $query) or die( print_r( sqlsrv_errors(), true));

Answer2:

I believe you're missing the WHERE keyword. Is that suppose to say:

UPDATE table SET Title = 'Test' WHERE ID = 1

?

Answer3:

try

UPDATE table SET Title = 'Test', ID = 1

you miss the comma ","

Answer4:

I think you are trying either of this

UPDATE table SET Title = 'Test' WHERE ID = 1

OR

UPDATE table SET Title = 'Test', ID = 1

Answer5:

It is an old solution:

$result = sqlsrv_query($this->connection, $query) or die( print_r( sqlsrv_errors(), true));

But saved me miles, two years latter on!

Recommend

  • Can't Cannect to SQL Server 2008 Express Database
  • INSERT - PHP & SQL Server
  • Execute Success but num_rows return 0 [PHP-MySQL]
  • How to export MS Access table into a csv file in Python using e.g. pypyodbc
  • Displaying Data From Multiple MySQL Tables
  • React Native + Redux: What's the best and preferred navigation?
  • PHP - Can't dynamcally instantiate class
  • Use neo4j server instead of embedded mode
  • Netlink sockets and libnl - nl_recvmsgs_default returning -16 (EBUSY)
  • Recreate the Oracle DUAL table
  • Kivy, TypeError: 'NoneType' object has no attribute '__getitem__'
  • Specify HTTPS for custom WCF Binding
  • JSON - slashes not escaping
  • Converting query results into DataFrame in python
  • Time out Error in send mail
  • How can I get the choice “H2” back in the H2 consol?
  • pip in virtualenv gets ConnectTimeoutError
  • Dynamically switching connect in Modelica
  • How to remove a SwiftyJSON element?
  • Security issues with PHP's Readfile method
  • nonblocking BIO_do_connect blocked when there is no internet connected
  • Redux Form - Not able to type anything in input
  • How can I extract results of aggregate queries in slick?
  • Get history of file changes from TFS to implement custom “blame”-behaviour of exceptions
  • Needing to do .toArray() to get output of mongodb .find() on key name not value
  • onBackPressed() not being executed
  • MongoDB in PHP using aggregate to group by _id is null not working
  • Delete MySQLi record without showing the id in the URL
  • Turn off referential integrity in Derby? is it possible?
  • How can I remove ASP.NET Designer.cs files?
  • sending mail using smtp is too slow
  • Programmatically clearing map cache
  • Busy indicator not showing up in wpf window [duplicate]
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal