Image insert like medium blob from page in mysql

Image insert like medium blob from page in mysql .Now inserting like binary code in row image i want in row images like blob . this is code

<?php
// Create database connection
$db = mysqli_connect("localhost", "root", "", "");

// Initialize message variable
$msg = "";

// If upload button is clicked …
if (isset($_POST['upload'])) {
// Get image name
$image = $_FILES['image']['name'];
// Get text
if(!empty($_POST['images'])){
$images= $_POST['images'];

$image_text = mysqli_real_escape_string($db, $_POST['image_text']);
}
// image file directory
$target = "images/".basename($image);

$sql = "INSERT INTO blog1 (images) VALUES ('$image')";
// execute query
mysqli_query($db, $sql);
 if(!empty($_POST['images'])){
$images= $_POST['images'];
if (move_uploaded_file($_FILES['images']['tmp_name'], $target)) {
}

$msg = "Image uploaded successfully";
}else{
$msg = "Failed to upload image";
}
}
$result = mysqli_query($db, "SELECT * FROM images");
?>

i tried this but wont display <?php echo "<img src= ".$row_Recordset1[‘image’]. "’ > alt=‘jbg’ ";

I don’t think keeping images in blobs is efficient. Some may say it is, but I disagree with this approach. It is more simple to store the file name and set it as a regular text or varchar data type. Then all you really got to do is pull the file name from the database, check to see if the file actually exists, then display the image using just the file name. A simple example of what I do is

$sql = 'SELECT image FROM photos WHERE id = 1';

Then I’d just do something like

if(file_exists($photoLocation . $photos->image)) {
?>
<img src="https://domainnamehere/photo_location_here/<?php print($photos->image); ?>">
<?PHP
} else {

    // Do something here instead

}

Simple as that. You don’t even need to nest your HTML in PHP because you can simply escape out of PHP and back into HTML. You reduce the amount of mistakes you make when you don’t nest multiple languages together.

nothing happen

That’s not a very descriptive explanation. What didn’t happen? What did you use? What changes did you make?

1 Like
$sql = 'SELECT image FROM blog WHERE id = 1';
if(file_exists($imagesLocation . $images->image)) {
?>
<img src="http://localhost/h/l/cv/blog-post-dark.php<?php print($images->image); ?>">
<?PHP
} else {

    // Do something here instead

}

I’m guessing the choke is the parentheses in the file names. It may be valid, I don’t know because I’ve never tried anything like filename(2).jpg, but I have a “feeling”.

I’m also guessing you didn’t try anything to help debug where the “// do something here” is. Try temporarily displaying the variable values to see what they look like. eg.

} else {
    // Do something here instead
  $location_image = $imagesLocation . $images->image; 
  var_dump($location_image);
}

tried temporarily displaying the variable values

and … ?

nothing
happen

Then you must not have tried in the correct place. Look at your code to locate the correct place and try again please. Then post what it displays.

echo '<img class="comment-avatar pull-left" src="data:jpeg;base64,'.base64_encode($row['images']).'" float: left; >';

free of the errors

 echo "<img class="comment-avatar pull-left" src='images/".$row['images']."'>";

it has errors

what i am missing i need second echo

Well, you seem to be jumping around instead of being methodical which doesn’t help me much. But anyway, disregarding the code you posted a few previous and dealing with the code you just posted, take a close look at the “it has errors” line. Notice where it changes between red and black? In this case the red highlighting indicates a string and black/blueish indicates code. See the problem? You need to use different types of quotation marks (i.e. double-single or single-double) or backslash escape the similar quotation marks that are nested within the outer quotation marks.

I am not beginner i am doing this for long time but sometimes code act strangely

That’s usually when the programmer has mad a mistake.

1 Like

The sample code I gave you wasn’t supposed to work. Especially because the sample code I am using is a generalized code. It was never meant to work except there for you to reference some parts. That’s what I was trying to get across hence me saying

how to get images here http://localhost/mages/

You upload it to there?

how
i dont know