2

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?

Jason
  • 1,787
  • 4
  • 29
  • 46

1 Answers1

2

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 = ?";
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100