Я использую этот код, но он вызывает ошибку: SQLSTATE: 42000 code: 156 сообщение: [Microsoft] [драйвер ODBC 13 для SQL Server] [SQL Server] Неверный синтаксис рядом с ключевым словом «файл».
$serverName = "HREXPRESS";
$connectionInfo = array( "Database"=>"hrexpress"/* "UID"=>"username",
"PWD"=>"password"*/ );
$conn = sqlsrv_connect( $serverName, $connectionInfo );
if( $conn === false ) {
echo "Could not connect.
";
die( print_r( sqlsrv_errors(), true));
}
include "$_SERVER[DOCUMENT_ROOT]/Classes/PHPExcel.php";
include "$_SERVER[DOCUMENT_ROOT]/Classes/PHPExcel/IOFactory.php";
//load excel file using PHPExcel's IOfactory
$excel = PHPExcel_IOFactory::load('file2.xls');
//set active sheet to first sheet
$excel->setActiveSheetIndex(0);
echo "<table>";
echo "
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>age</th>
</tr>
";
//first row of data series
$i = 1 ;
//loop until the end of data series(cedll contains empty string)
while($excel->getActiveSheet()->getcell('A'.$i)->getValue() != ""){
//get cells value
$id = $excel->getactiveSheet()->getCell('A'.$i)->getValue();
$first = $excel->getactiveSheet()->getCell('B'.$i)->getValue();
$last = $excel->getactiveSheet()->getCell('C'.$i)->getValue();
$age = $excel->getactiveSheet()->getCell('D'.$i)->getValue();
$q = "INSERT INTO dbo.file (id,first,last,age) VALUES ('$id','$first','$last','$age')";
$d = sqlsrv_query($conn,$q);
//echo
echo "
<tr>
<td>".$id."</td>
<td>".$first."</td>
<td>".$last."</td>
<td>".$age."</td>
</tr>
";
//and DON'T FORGET to increment the row pointer ($i)
$i++;
}
echo "</table>";
//echo error
if( $d === false ) {
if( ($errors = sqlsrv_errors() ) != null) {
foreach( $errors as $error ) {
echo "</br>SQLSTATE: ".$error[ 'SQLSTATE']."<br />";
echo "code: ".$error[ 'code']."<br />";
echo "message: ".$error[ 'message']."<br />";
}
}
}