I'm having problems putting data into MSSQL with PHP - I get this error:
sqlsrv_query() expects parameter 1 to be resource, boolean given in [path] on line 13
I can't really see where the problem is, or if I have a syntax error.
<?php
$host = "127.0.0.1";
$dbusername = "humid";
$dbpassword = "humid";
$db = "test";
$t = $_GET['t'];
$h = $_GET['h'];
$connectionInfo = array("UID" => $dbusername, "PWD" => $dbpassword, "Database" => $db) or die("Couldnt connect to server");
$dbconnect = sqlsrv_connect($host, $connectionInfo);
$sql = "INSERT INTO historic VALUES(NULL,$t,$h,CURRENT_TIMESTAMP)";
sqlsrv_query($dbconnect, $sql);
?>
ANY help would be greatly appreciated! I'm not familiar with MSSQL, only MySQL
Okay, now I've enabled error messages, and I've got this one now:
Array ( [0] => Array ( [0] => 42000 [SQLSTATE] => 42000 [1] => 126 [code] => 126 [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid pseudocolumn "$t". [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Invalid pseudocolumn "$t". ) )
However, I have no idea what that could be? Is it because $t is a SQL syntax that I don't know of? Long story short, I'm building a temp and humid logging device from an Arduino, and I need it to post the values into a DB (using PHP) - the syntax for the PHP looks like this:
test.php?t=1&h=2
What have i done wrong now? When testing it on my MySQL server private, it all worked fine >_<
-- edit, solution found! --
Okay, I found the solution, I'm not sure if any of the other answers had a direct fix, however. MSSQL apparently doesn't accept "NULL" like MySQL does. But that was ofc. the last thing i edited, so I'm not sure if there were any errors in the prior code. Hoever, below is the final (and working) code. Arduino posts and everything. Eureka!
<?php
$host = "WDKTO560\TEST";
$dbusername = "humid";
$dbpassword = "humid";
$db = "test";
$t = $_GET['t'];
$h = $_GET['h'];
$connectionInfo = array("UID" => $dbusername, "PWD" => $dbpassword, "Database" => $db) or die("Couldnt connect to server");
$dbconnect = sqlsrv_connect($host, $connectionInfo);
$sql = "INSERT INTO historic (temperature, humidity, date) VALUES($t,$h,CURRENT_TIMESTAMP)";
sqlsrv_query($dbconnect, $sql) or die( print_r( sqlsrv_errors(), true));
?>