0

Good morning, I'm trying to download a blob image from mysql for an employee form. When downloading it, it shows me the empty field with the first code that I leave below:

Code 1: enter image description here

<?php
    $con = mysql_connect ("sql213.epizy.com","epiz_31980107","l20DHKEhYd");
    if (!$con){die ("ERROR AL CONECTAR CON LA BASE DE DATOS ".mysql_error());}
    $db = mysql_select_db("epiz_31980107_certificaciones",$con);
    if (!$db) {die ("ERROR AL SELECCIONAR DB ".mysql_error());}


 //ojo: con la sentencia sql, que es la que 
 //utilizamos para la consulta a mas de una tabla

 $sql = ("SELECT empleado.NoReloj,certificaciones.Certificacion,certificaciones.Fecha_Cert,certificaciones.Fecha_Ven 
                FROM empleado, certificaciones WHERE empleado.NoReloj='".$_REQUEST['matricula']."' AND certificaciones.empleado_id ='".$_REQUEST['matricula']."'");

 //realizamos la consulta
 $consulta2= ("SELECT * FROM empleado WHERE empleado.NoReloj='" .$_REQUEST['matricula']."'");
$consuimg= ("SELECT Imagen FROM empleado  WHERE empleado.NoReloj='" .$_REQUEST['matricula']."'");

 $result2=mysql_query($consulta2);
$resuimg=mysql_query($consuimg);
 $result = mysql_query($sql,$con);


 if(mysql_num_rows($result2)>0){

    if($datos=mysql_fetch_row($result2)){
        $matricula2=$datos[0];
        $img=$datos[1];
        $nombre2=$datos[2];
        $apellido1=$datos[3];
        $apellido2=$datos[4];
        $fecha=$datos[5];
        $area=$datos[6];
        
        
    }
 
 }
 

 ?>

 <table align="center">
  
 <tr>
    <td>Numero de Reloj</td>
    <td>Area</td>
 </tr>
 <tr>
 <td><input type="text" name="matricula" value="<?php echo $matricula2; ?>" disabled></td>
 <td><input type="text" value="<?php echo $area; ?>" disabled></td>
 <img value="<?php echo $img; ?>" />

But when I try it with the code2 that I leave below, it throws me the image as broken that it is not being able to recover it. I would be very grateful if you could help me to know what I am doing wrong.

Thanks in advance

Code2:
enter image description here

<?php
    
    $con = mysql_connect ("sql213.epizy.com","epiz_31980107","l20DHKEhYd");
    if (!$con){die ("ERROR AL CONECTAR CON LA BASE DE DATOS ".mysql_error());}
    $db = mysql_select_db("epiz_31980107_certificaciones",$con);
    if (!$db) {die ("ERROR AL SELECCIONAR DB ".mysql_error());}


 //ojo: con la sentencia sql, que es la que 
 //utilizamos para la consulta a mas de una tabla

 $sql = ("SELECT empleado.NoReloj,certificaciones.Certificacion,certificaciones.Fecha_Cert,certificaciones.Fecha_Ven 
                FROM empleado, certificaciones WHERE empleado.NoReloj='".$_REQUEST['matricula']."' AND certificaciones.empleado_id ='".$_REQUEST['matricula']."'");

 //realizamos la consulta
 $consulta2= ("SELECT * FROM empleado WHERE empleado.NoReloj='" .$_REQUEST['matricula']."'");
$consuimg= ("SELECT Imagen FROM empleado  WHERE empleado.NoReloj='" .$_REQUEST['matricula']."'");
 $result2=mysql_query($consulta2);
 $result = mysql_query($sql,$con);
$resimg = mysql_query($consuimg,$con);


 if(mysql_num_rows($result2)>0){

    if($datos=mysql_fetch_row($result2)){
        $matricula2=$datos[0];
        $nombre2=$datos[2];
        $apellido1=$datos[3];
        $apellido2=$datos[4];
        $fecha=$datos[5];
        $area=$datos[6];
        
        
    }
 } 
 while($row = mysql_fetch_assoc($resimg)) {
  ?>
  <img src="data:<?php echo  base64_encode($row['Imagen']); ?>">

 

 <?php
 }

  
 ?>

 <table align="center">

 <tr>
    <td>Numero de Reloj</td>
    <td>Area</td>
 </tr>
 <tr>
 <td><input type="text" name="matricula" value="<?php echo $matricula2; ?>" disabled></td>
 <td><input type="text" value="<?php echo $area; ?>" disabled></td>
Markus Meyer
  • 3,327
  • 10
  • 22
  • 35
Alberto Cruz
  • 101
  • 2
  • 1
    Why are you using the obsolete `mysql_` code library? It was discontinued in PHP 5.5 (2013) and removed entirely in PHP 7 (2015). No apps should still be using it. It leaves you vulnerable to SQL injection attacks (due to the lack of parameterised query support) and potentially other unpatched vulnerabilities. Switch to using `mysqli` or `PDO` (both released in the 2000s!) as soon as possible, and learn how to write parameterised queries to protect your data. See http://bobby-tables.com for a simple explanation of the risks and some sample PHP code to write queries safely. – ADyson Jul 05 '22 at 13:57
  • Also, if you are running `mysql_` queries successfully, it shows you are running an unsupported version of PHP. You should upgrade your PHP version urgently to continue to receive security updates. https://www.php.net/supported-versions.php – ADyson Jul 05 '22 at 13:57
  • 1
    https://phpdelusions.net/mysqli also contains good examples of writing safe SQL using mysqli. See also the [mysqli documentation](https://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php) and this: [How can I prevent SQL injection in PHP?](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) . Parameterising your queries will also greatly reduce the risk of accidental syntax errors as a result of un-escaped or incorrectly quoted input values. – ADyson Jul 05 '22 at 13:58
  • Hopefully those are fake credentials, if not reset ASAP. – user3783243 Jul 05 '22 at 13:58
  • 2
    Anyway we don't know if `$row['Imagen']` contains any valid data or not, or what the output of running base64_encode on it is. Normally you need to prepend more than just `data` to make it display in an img tag though. – ADyson Jul 05 '22 at 14:00
  • 1
    Does this answer your question? [Displaying a Base64 images from a database via PHP](https://stackoverflow.com/questions/16262098/displaying-a-base64-images-from-a-database-via-php) – ADyson Jul 05 '22 at 14:00
  • Your image source also would be incorrect, or possibly, not sure what DB has.. see https://stackoverflow.com/questions/16262098/displaying-a-base64-images-from-a-database-via-php likely better soultion is storing images on server and keeping location in DB. – user3783243 Jul 05 '22 at 14:01
  • So I would have to redo all my code from scratch to adapt it to mysqli right? – Alberto Cruz Jul 05 '22 at 14:20
  • Well, the image is saved as a BLOB type in my database, the problem is that it doesn't show it, I don't know if it's due to a bad query or a writing problem in my code. – Alberto Cruz Jul 05 '22 at 14:22
  • @AlbertoCruz Did you click either link to read proper way to call a blob/base64 encoded image from DB? – user3783243 Jul 05 '22 at 14:24
  • @user3783243 If in fact I modified my code in this way but still it does not show me the image. – Alberto Cruz Jul 05 '22 at 14:27
  • @user3783243 $consuimg= ("SELECT Imagen FROM empleado WHERE empleado.NoReloj='" .$_REQUEST['matricula']."'"); $resimg = mysql_query($consuimg,$con); while($row = mysql_fetch_assoc($resimg)){ ?> – Alberto Cruz Jul 05 '22 at 14:28
  • @AlbertoCruz Image in DB is `jpeg`? What does browser console show? – user3783243 Jul 05 '22 at 14:36
  • @user3783243 No it is not, but it changes the jpeg to png and still does not display the image. – Alberto Cruz Jul 05 '22 at 14:49
  • @AlbertoCruz If you don't have a JPEG don't say it is a JPEG, `image/jpeg`. It would be far easier to store image location and output that to the source..if not you should consult a tutorial. Seems like you are confused about a few things. – user3783243 Jul 05 '22 at 14:55
  • @user3783243 No, I'm not confused, just delete all previously entered records and enter new users and just forget to change the jpeg to the png. And I already checked all the possible tutorials about blob with type png gif jpg and jpeg but I can't extract the image to my form. – Alberto Cruz Jul 05 '22 at 15:02
  • `I would have to redo all my code from scratch to adapt it to mysqli`...yes, but you've had 10 years to think about it already (since `mysql_` was deprecated), so hopefully you have a plan :-). As we've shown, there are guides you can follow. I've linked mysqli as it's most similar in some ways, but also PDO is good, and a lot of people find it easier to use than mysqli. – ADyson Jul 05 '22 at 16:50
  • @ADyson Ok i will have to readjust everything. – Alberto Cruz Jul 05 '22 at 17:47

0 Answers0