使用php插入sql server数据库时出错

时间:2016-11-02 12:58:06

标签: php sql sql-server database insert

我在使用PHP将多列数据插入SQL Server数据库时出错

php代码:

<?php
    $serverName = "GHAREBAGHI\sqlexpress, 1433";
    $connectionInfo = array( "Database"=>"PwKara", "UID"=>"test","PWD"=>"100100");
    $conn = sqlsrv_connect( $serverName, $connectionInfo);

    if( $conn ) {
        echo "Connection established.<br />";
    } else {
        echo "Connection could not be established.<br />";
        die( print_r( sqlsrv_errors(), true));
    }
    $tsql= "INSERT INTO Requests
  (EMP_NO ,SubmittedDate ,Type ,StartDate ,EndDate ,StartHour ,EndHour
,Duration ,SubmittedByEmployeeID ,OperationsID ,Description ,CurEmp_NO ,AcceptCode ,IsWardenCheck ,RequestStatus ,IOStatus ,CurSection)
VALUES
(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    $para = array("93229 ,'2016-10-19 09:23:48.850' ,54 ,'2016-10-19 09:23:48.850' ,'2016-10-19 09:23:48.850' ,2359 ,2359 ,2 ,83229 ,3 ,'php test' ,8813 ,0 ,0 ,0 ,0 ,6");

    if (!sqlsrv_query($conn, $tsql, $para)){
        die('Error: ' . sqlsrv_errors());
    }
    echo "1 record added"; 
?>

结果:php error code(第52行的数组到字符串转换)

错误指向他的行:

die('Error: ' . sqlsrv_errors());

1 个答案:

答案 0 :(得分:0)

$para = array为什么那些" .... "?这不是数组,这是字符串。双方都要删除"。它会像:

$para = array(93229, '2016-10-19 09:23:48.850', 54, '2016-10-19 09:23:48.850', '2016-10-19 09:23:48.850', 2359, 2359, 2, 83229, 3, 'php test', 8813, 0, 0, 0, 0, 6);