【问题标题】:PHP PDO & Large Objects (LOB) broken after updatePHP PDO 和大对象 (LOB) 更新后损坏
【发布时间】:2017-08-01 22:42:36
【问题描述】:

几个月前,我的 Ubuntu 软件包自动将 PHP 从 7.0.8 更新到 7.0.13,此时我用于更新存储在 SQL 数据库中的照片的脚本开始失败。我通过重新安装 7.0.8 解决了这个问题。上个月,我再次自动更新到 7.0.15,我的脚本再次失败。

我的脚本使用 PDO 和 FreeTDS 以及大对象 (LOB) 将 jpg 图像写入 MS-SQL 数据库来处理照片。我强调它适用于 PHP 版本 7.0.8。以下是隔离我的问题的测试脚本。

<?php

$dsn = 'dblib:dbname=photos;host=gary';
$id = 693925;

$dbh = new PDO($dsn, $user, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

try {    
       $photo = file_get_contents("coco.jpg");
       $query = "UPDATE photo_table SET photo = :photo WHERE id = :id";
       $stmt = $dbh->prepare($query);
       $stmt->bindValue(":photo", $photo, PDO::PARAM_LOB);
       $stmt->bindValue(":id", $id, PDO::PARAM_INT);
       $stmt->execute();
    }
}
catch (PDOException $e) {
    echo $e->getMessage();
}

结果是“语法错误”错误!?

SQLSTATE[HY000]: General error: 
102 Incorrect syntax near '����'.[102] (severity 15) [(null)]

使用最新可用的 PHP 版本 7.0.15,从数据库中读取工作,包括将照片作为大对象读取。将所有其他字段写入数据库没有问题,它只在我的图像上失败。

尽管过去几周进行了搜索,但我仍然需要找到其他人报告同样的问题。

我正在寻求任何建议,要么更改代码,要么进行一些配置设置以允许 LOB 再次工作。

【问题讨论】:

标签: php sql-server pdo


【解决方案1】:

我建议您始终使用 bindParam 而不是 bindValue 因为在 bindParam

PDOStatement::bindValue() 不同,变量绑定为 参考并且只会在那个时候被评估 PDOStatement::execute() 被调用。

   $photo = file_get_contents("coco.jpg");//change this to below
   $photo = fopen($_FILES['file']['tmp_name'], 'rb');

   $query = "UPDATE photo_table SET photo = :photo WHERE id = :id";
   $stmt = $dbh->prepare($query);

   $stmt->bindValue(":photo", $photo, PDO::PARAM_LOB);//change to this below
   $stmt->bindParam(":photo", $photo, PDO::PARAM_LOB);

   $stmt->bindValue(":id", $id, PDO::PARAM_INT);//change this to below
   $stmt->bindParam(":id", $id, PDO::PARAM_INT);

这只是建议检查这里......http://php.net/manual/en/pdo.lobs.php & http://www.php.net/manual/en/pdostatement.bindparam.php#refsect1-pdostatement.bindparam-description

【讨论】:

  • 与 bindParam 的结果相同 :(
【解决方案2】:

我的解决方案/解决方法是在将数据发送到 SQL 之前,将图像中的二进制转换为十六进制表示。

$photo = bin2hex(file_get_contents("coco.jpg"));

在 SQL 语句期间再次将其转换回来。

$query = 
"UPDATE photo_table SET photo=CONVERT(varbinary(max), :photo, 2) WHERE id = :id";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-16
    • 2020-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-19
    相关资源
    最近更新 更多