因此我们希望将pdf文件从目录插入数据库。
这是错误:
PHP致命错误:未捕获PDOException:SQLSTATE [42000]:[Microsoft] [SQL Server的ODBC驱动程序13] [SQL Server]' @ P1'附近的语法不正确。在D:\ dir \ dir \ dbData.php:662
代码:
dbData.php:
public function insFile($filename)
{
$db = getDB();
$query = $db->prepare("
INSERT INTO images(id, imageFile)
SELECT NEWID(), BulkColumn
FROM OPENROWSET(BULK :filename, SINGLE_BLOB) as f;
");
$query->bindParam(':filename', $filename, PDO::PARAM_STR);
$data = $query->fetchAll(PDO::FETCH_OBJ);
$query->execute(); // THIS IS THE 662ND LINE
$db = null;
return null;
}
和
home.php
<?php
require_once(_DIR_.'/'.'oconfig.php');
foreach (glob("D:/dir/dir/pdf/*.pdf") as $filename) {
$filename = "D:/dir/dir/pdf/" . basename($filename);
$userData=$dbData->insFile($filename);
echo $userData;
}
?>
答案 0 :(得分:0)
如上所述,SQL Server的OPENROWSET中的BULK
filename 字符串文字无法参数化。相反,请考虑将文件名直接连接到迭代循环运行的调用中。
public function insFile($filename)
{
$db = getDB();
$query = $db->query("
INSERT INTO images(id, imageFile)
SELECT NEWID(), BulkColumn
FROM OPENROWSET(BULK '$filename', SINGLE_BLOB) as f;
");
$db = null;
return null;
}