I have a code, and this code call some datas of my MySql database and then show it in a <p>
, it works perfectly, but when i want to call an image that is saved in a mediumblob type, it does not work.
Here's the code
Ajax.js
$(document).on("ready", function() {
$("#select").on("change", function(e) {
var option = $(this).val();
if(option.toLowerCase() !== "ninguno") {
var dataToSend = JSON.parse('{"name": "reference", "value": "'+option+'"}');
ajaxProcess(dataToSend);
} else {
$("p").each( function() {
$(this).html("");
});
}
});
function ajaxProcess(dataToSend) {
$.ajax({
url: "process.php",
dataType: "json",
data: dataToSend,
type: "get"
}
)
.done( function(data) {
$("#ram").html(data["ram"]);
$("#camara").html(data["camara"]);
$("#imagen").html(data["imagen"]); //Here's the problem
})
.fail( function(jqXHR,textStatus,errorThrown) {
console.log("Can´t get the data");
});
}
});
When i select the value in a combobox, it shows the error message "Can´t get the data"
Process.php
<?php
$connection = new mysqli("127.0.0.1","root","","labash");
$connection->set_charset("utf8");
if($connection->connect_errno)
throw new Exception("Error al conectar a la base de datos");
$stmt = $connection->stmt_init();
$query = "SELECT ram,camara,imagen FROM celulares WHERE reference=?";
if($stmt->prepare($query)) { // prepare the sentence
$name = $_GET["value"];
$stmt->bind_param("s", $name); //bind data
$stmt->execute(); // execute the sentence
$result = $stmt->get_result(); // get the result
$connection->close();
echo json_encode($result->fetch_assoc()); // get the data as JSON
}
In the html code i call the "ram" data (for example) in a <p id="ram">
and it works, but the image doesn´t. Thanks Beforehand
I solve it
First: I use a converter Base64 image
Then i copy the code and i save that code in the database
And here´s the new code of the Ajax.js file:
$("#imagen").attr('src', data["imagen_encode"]);
imagen_encode
is the new column of my table in the database. i paste all the code of the Base64 converter and save it in the imagen_encode
column. And i call it in html <img id="imagen">