我正在尝试在我的SQL服务器的'products'表中插入数据。我无法使用我的代码填充表格。我不知道我的代码中有什么错误。
以下是我的代码:
<?php
/* Specify the server and connection string attributes. */
$serverName = "DBIT-NB1415546\SQLEXPRESS(SQL Server 1)"; // Change this to the name of your own SQL Server
$connectionInfo = array( "Databases"=>"CA1_OLTP");
/* Connect using Windows Authentication. */
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false ) {
echo "Unable to connect.</br>";
die( print_r( sqlsrv_errors(), true));
}
//read JSON data from the cloud DB
$json = file_get_contents("http://bi.edisonsiow.com/ay1516s2/ca1/getProducts.php");
$jsonObject = json_decode($json);
//go through each record in the JSON object and create a record in employees table
//in MS SQL Server DB
foreach ($jsonObject as $key => $item ){
$sql = "INSERT INTO products(productCode, productName,".
"productLine,productScale,productVendor,".
"productDescription,quantityInStock,buyPrice,MSRP)".
"VALUES(?,?,?,?,?,?,?,?,?)";
$param = array(&$item->productCode, &$item->productName, &$item->productLine, &$item->productScale,
&$item->productVendor, &$item->productDescription, &$item->quantityInStock, &$item->buyPrice,
&$item->MSRP);
$stmt = sqlsrv_prepare($conn, $sql, $param);
if( sqlsrv_execute($stmt) === false ) {
$errors = sqlsrv_errors();
if($errors[0]['code']==2627) {
die("Employee already exist in employeeDIM. No duplication is allowed.");
} //end inner if
} //end if
}
echo "<h3>Records Created..</h3>";
?>
答案 0 :(得分:1)
我认为问题可能是$ serverName的实例名称。我不认为服务器名称应该有括号。有关如何在此处检查服务器名称的实例链接: