douou5933 2017-03-11 16:43
浏览 27
已采纳

更新后PHP PDO和大对象(LOB)中断

A few months ago, my Ubuntu package auto-updated PHP from 7.0.8 to 7.0.13, at which point my script for updating photos stored on a SQL database started failing. I got around this by reinstalling 7.0.8. Last month, I was again auto-updated to 7.0.15 and my script failed again.

My script writes a jpg image to a MS-SQL database, using PDO & FreeTDS, plus Large Objects (LOB) to handle the photo. I emphasise that it worked up to PHP version 7.0.8. The following is a test script isolating my problem.

<?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();
}

The result is an "Incorrect syntax" error!?

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

Using the latest available PHP version 7.0.15, reading from the database works, including reading the photo as a Large Object. There is no problem writing every other field to the database, it only fails on my image.

Despite searching over the last few weeks, I still have to find someone else reporting the same problem.

I am after any advice, either a change to the code, or some configuration settings to allow LOBs to work again.

  • 写回答

2条回答 默认 最新

  • duanbimo7212 2017-03-12 12:41
    关注

    My solution/workaround was to convert the binary from the image into hexadecimal representation before sending the data to SQL.

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

    converting it back again during the SQL statement.

    $query = 
    "UPDATE photo_table SET photo=CONVERT(varbinary(max), :photo, 2) WHERE id = :id";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来