简体   繁体   English

从PHP到MSSQL的连接问题

[英]Issue with connection to MSSQL from PHP

I'm having problems putting data into MSSQL with PHP - I get this error: 我在使用PHP将数据放入MSSQL时遇到问题-我收到此错误:

sqlsrv_query() expects parameter 1 to be resource, boolean given in [path] on line 13 sqlsrv_query()期望参数1为资源,在第13行的[path]中给出布尔值

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 我不熟悉MSSQL,只有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". ) ) 数组([0] =>数组([0] => 42000 [SQLSTATE] => 42000 [1] => 126 [代码] => 126 [2] => [Microsoft] [用于SQL Server的ODBC驱动程序11] [ SQL Server]无效的伪列“ $ t”。[消息] => [Microsoft] [SQL Server的ODBC驱动程序11] [SQL Server]无效的伪列“ $ t”。))

However, I have no idea what that could be? 但是,我不知道那会是什么? Is it because $t is a SQL syntax that I don't know of? 是否因为$ t是我不知道的SQL语法? 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: 长话短说,我正在用Arduino构建温度和湿度记录设备,我需要它将值发布到DB中(使用PHP)-PHP的语法如下:

test.php?t=1&h=2 test.php?t = 1&h = 2

What have i done wrong now? 我现在做错了什么? When testing it on my MySQL server private, it all worked fine >_< 在我的MySQL服务器专用服务器上对其进行测试时,一切正常> _ <

-- 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. MSSQL显然不像MySQL那样接受“ NULL”。 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. Arduino帖子以及所有内容。 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));
?>

sqlsrv_connect() is failing to get a connection and returning FALSE. sqlsrv_connect()无法获得连接并返回FALSE。 You will have to debug it. 您将不得不调试它。

  1. Check if database and Username is correct . 检查数据库和用户名是否正确。
  2. User has access permission to write into DATABASE 用户具有写入数据库的访问权限
  3. You have installed the MSSQL driver for PHP. 您已经安装了用于PHP的MSSQL驱动程序。

Hope this helps. 希望这可以帮助。

Connect using Windows Authentication. 使用Windows身份验证进行连接。

<?php
    $serverName = "serverName\sqlexpress"; //serverName,instanceName


    $ConInfo = array( "Database"=>"dbName");
    $conn = sqlsrv_connect( $serverName, $ConInfo);

    if( $conn ) {
         echo "Connection established.<br />";
    }else{
         echo "Connection could not be established.<br />";
         die( print_r( sqlsrv_errors(), true));
    }
    ?>

Connect by user name and password. 通过用户名和密码连接。

 <?php
    $serverName = "serverName\sqlexpress";
    $ConInfo = array( "Database"=>"dbName", "UserId"=>"userName", "PWD"=>"password");
    $conn = sqlsrv_connect( $serverName, $ConInfo);

    if( $conn ) {
         echo "Connection established.<br />";
    }else{
         echo "Connection could not be established.<br />";
         die( print_r( sqlsrv_errors(), true));
    }
    ?>

Hope this helps 希望这可以帮助

The answer was actually quite simple, it was a syntax error. 答案实际上很简单,这是一个语法错误。 MSSQL does NOT accept NULL to af inserted, but MySQL does. MSSQL不接受NULL插入af,但是MySQL接受。 I wasn't familiar with MSSQL, so thats why I didn't saw the error. 我不熟悉MSSQL,所以这就是为什么我没有看到错误的原因。

So, the correct syntax for that line is: 因此,该行的正确语法为:

$sql = "INSERT INTO historic (temperature, humidity, date) VALUES($t,$h,CURRENT_TIMESTAMP)";

I simply had to specify the columns (the first table was ID, auto incremented) and remove the NULL. 我只需要指定列(第一个表是ID,自动递增)并删除NULL。 In MySQL NULL would skip that column. 在MySQL中,NULL将跳过该列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM