Selecting Blob from MYSQL, getting null

笑着哭i 提交于 2019-12-11 01:39:23

问题


Is there anything special I need to do when selecting a blob from MySQL? I get a NULL for the following (data is a mediumblob, file_extension is varchar):

$sql = "SELECT data, file_extension FROM table1 WHERE table1.id = ?";
$q = $con->prepare($sql);
$q->execute(array(1));

if ($q->rowCount() == 0)
{
    disconnectSqlConnection($con);
    $arr = array('success' => 'false',
                 'error'   => -2);
    return json_encode($arr);
}
else
{
    $row = $q->fetch();
    $arr = array('success'   => 'true',
                 'data'      => $row[0],
                 'extension' => $row[1]);
    disconnectSqlConnection($con);
    return json_encode($arr);
}

$row[1] has 'pdf', which is correct. $row[0] is NULL, but in the database, I see a blob of size 244.8KB

Any thoughts?


回答1:


use CAST to convert it to text

$sql = "SELECT CAST(data AS CHAR(10000) CHARACTER SET utf8) as data, file_extension FROM table1 WHERE table1.id = ?";


来源:https://stackoverflow.com/questions/15948798/selecting-blob-from-mysql-getting-null

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!