0

I've migrated a access database with images on its fields to mysql.

When I try to visualize them with several php codes I get a broken image icon or I download php code (PHP: Retrieve image from MySQL using PDO) that I tried to use in a new try:

<?php
$con = mysqli_connect('localhost', 'root', '', 'access');
$query = mysqli_query($con,"SELECT EscudoClub FROM tclubs WHERE CodClub =    'C13'");
$imageData = mysqli_fetch_array($query, MYSQLI_ASSOC);
$image = $imageData['EscudoClub'];
header("Content-type: image/jpeg");
echo $image;
mysqli_free_result($query);
mysqli_close($con);

?>

With above code I get a broken image icon and using pdo I only get dowwnload php code I guess because some syntax problems:

//$dbName = $_SERVER["DOCUMENT_ROOT"]."\\..\db\\teknofo.mdb";
//$con = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=access;   Uid=; Pwd=;");
$con = new PDO('mysql:host=localhost;dbname=access;charset=utf8', 'root', '');
$sql = "SELECT EscudoClub FROM tclubs WHERE CodClub = 'C13'";
$st = $con->prepare($sql);
$st->execute(array(17));
$st->bindColumn('photo', $photo, PDO::PARAM_LOB);
$st->fetch(PDO::FETCH_BOUND);
odbc_longreadlen($st, 131072);        
odbc_binmode($st,ODBC_BINMODE_CONVERT);                            
ob_clean();
header('Content-Type: image/*');
if ($rd = $st->fetch(PDO::FETCH_BOUND)) 
{
echo $rd['photo'];
ob_end_flush();
$con = null;
}
?>

Please, could you help me with this?

Kind regards

Community
  • 1
  • 1
  • the sql variable should store the image. I read in original post that code works for displaying images here http://stackoverflow.com/questions/22325904/php-pdo-ms-access-how-to-read-blob-images. How could I change one of the 2 above codes to correctly display the images stored in sql variable? – Pablo Valcárcel Mar 09 '15 at 10:05

3 Answers3

1

When executing your statement, you provide an explicit parameter value (of 17)—but the statement does not contain any parameter placeholders! You're then attempting to bind a column named 'photo', which doesn't exist in the resultset. The odbc_* calls shouldn't be there either.

$con = new PDO('mysql:host=localhost;dbname=access;charset=utf8', 'root', '');
     // DON'T USE ROOT USER !!!

$st = $con->prepare('SELECT EscudoClub FROM tclubs WHERE CodClub = ?');
$st->execute(array('C13'));
if ($rd = $st->fetch())
{
  header('Content-Type: image/*'); // you should give an exact MIME type
  echo $rd['EscudoClub'];
}
eggyal
  • 122,705
  • 18
  • 212
  • 237
  • Hi.I used your code but I keep displaying a small icon (generic thumbnail). Root user is for test purposes only. I'm working with xampp. When I finish all migration with all access forms and queries I will secure mysql :). – Pablo Valcárcel Mar 09 '15 at 10:44
0

You should first map the $sql call from:

$sql = "SELECT EscudoClub FROM tclubs WHERE CodClub = 'C13'";

To :

"SELECT EscudoClub FROM tclubs WHERE CodClub = ':cod_club'";
$st = $con->prepare($sql);
$st->execute(array('cod_club' => 123456));

I don't know if this could solve your issue, but when you get straightly the PHP source code from a call, it is generally due to your server configuration (apache, nginx, etc.).

dam660
  • 79
  • 8
  • I tried with you code but I don't get anything on webpage – Pablo Valcárcel Mar 09 '15 at 10:47
  • What is your web server? Can you give us your configuration file? – dam660 Mar 09 '15 at 13:14
  • Hi. I'm working with xampp: Apache (2.4.10) and mysql (5.6). I'm not sure if it could be a data type migration problem as in access database those images were on microsoft word picture.format and now I have those images as blob on mysql. I have tested several php and finally I only get a small icon. I'm thinking in export those images and using uri reference on database but the problem is that I have a 1,3GB table – Pablo Valcárcel Mar 09 '15 at 15:49
  • Hm just saw a little mistake on the source code: try to remove the quotes in the select query, and bind with ':cod_club' (with the ':' ). Btw, have you succeeded in showing some other php pages? – dam660 Mar 11 '15 at 08:56
0

I would like to add that pictures on access database are stored as microsoft word pictures. I don't know if I should export them to any image format (jpeg, png..) before trying to display them. The problem is that I migrated all data from access database and there is a table with 1,3GB of photos.

Kind regards.