Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,211,061 members, 8,010,806 topics. Date: Saturday, 23 November 2024 at 08:31 AM

PHP Code To Insert And Retrieve Images From Mysql Server - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / PHP Code To Insert And Retrieve Images From Mysql Server (2466 Views)

Android, How To Display Image In Image View From Mysql / How Do I Retrieve Data With Apostrophe From Mysql Using PDO / My Php Code Does Not Execute But Displays Code On Browser, (2) (3) (4)

(1) (Reply) (Go Down)

PHP Code To Insert And Retrieve Images From Mysql Server by msal2016: 8:46am On May 18, 2016
Can someone put me through on this,i am developing e-commerce website that need to display the items picture on the webpage.
Re: PHP Code To Insert And Retrieve Images From Mysql Server by jacob05(m): 8:55am On May 18, 2016
Re: PHP Code To Insert And Retrieve Images From Mysql Server by bigtt76(f): 8:56am On May 18, 2016
Why suffer thy self? Why not use a ready and secured eCommerce application? In anycase, please try w3schhools.com or run a Google search for same.

msal2016:
Can someone put me through on this,i am developing e-commerce website that need to display the items picture on the webpage.
Re: PHP Code To Insert And Retrieve Images From Mysql Server by adexsimply(m): 9:09am On May 18, 2016
msal2016:
Can someone put me through on this,i am developing e-commerce website that need to display the items picture on the webpage.
shocked You want to build an ecommerce website from scratch?
Re: PHP Code To Insert And Retrieve Images From Mysql Server by sleepingdemon: 9:34am On May 18, 2016
must you store images in database, upload images to a folder in your web root, store the path in the database and your good to go.
but if ure keen in storing the image to database directly, use a blob field in your database, convert image to blob and save in database

1 Like

Re: PHP Code To Insert And Retrieve Images From Mysql Server by yusufabayomio(m): 9:58am On May 18, 2016
Hi, its better to use an existing ecommerce open source tools, than developing from the scratch. You can try:
OpenCart, Magento or WooCommerce (If you are conversant with WordPress)

msal2016:
Can someone put me through on this,i am developing e-commerce website that need to display the items picture on the webpage.
Re: PHP Code To Insert And Retrieve Images From Mysql Server by seunthomas: 2:37pm On May 18, 2016
Dont do that. Never save images in a database except the images are valuable and cant be kept on the filesystem. What you need to do is to upload the images to your filesystem, get the path of the file and save just that path information in the database. So if you uploaded the image to images/uploads/ in your application directory, then save the image/uploads/myimage.jpg in the database. Saving images as blob is very inefficient and like i said only do this if there is a very important reason that the file cant be kept on your filesystem.

1 Like

Re: PHP Code To Insert And Retrieve Images From Mysql Server by Nobody: 4:28pm On May 18, 2016
^^^I agree with the poster above me, OP listen to him or you will end up wasting your time!
Re: PHP Code To Insert And Retrieve Images From Mysql Server by jacob05(m): 4:35pm On May 18, 2016
^^^ I agree on what the above poster agreed on... grin grin grin
Re: PHP Code To Insert And Retrieve Images From Mysql Server by hollyfat(m): 4:44pm On May 18, 2016
adexsimply:
shocked You want to build an ecommerce website from scratch?
Is it impossible?
Re: PHP Code To Insert And Retrieve Images From Mysql Server by Nobody: 5:29pm On May 18, 2016
hollyfat:

Is it impossible?
It is possible, but I will advise you to write your WILL before you embark on it.
Re: PHP Code To Insert And Retrieve Images From Mysql Server by sleepingdemon: 5:53pm On May 18, 2016
grin grin its never that bad, jes buy holandia youghurt before you start building it, before you sip finish, you for don build the eCommerce app.
another tip op, make sure you buy gala too.
Re: PHP Code To Insert And Retrieve Images From Mysql Server by Nobody: 6:20pm On May 18, 2016
^^^And of course, he should also say his last prayers after chopping the gala (before embarking on the task)
Re: PHP Code To Insert And Retrieve Images From Mysql Server by lekropasky(m): 6:29pm On May 18, 2016
try This.........
<?php

mysql_connect("localhost", "your username", "your password"wink;
mysql_select_db("your_db_name"wink;

//get post Request
$image = $_POST['image'];
$image_name = $_FILES['imagename']['name'];
$image_path = "http://www.yoursitename.com/root/images".$image_name.".png";

$query = "INSERT INTO images(image_url) VALUES($image_path)";
if( move_uploaded_file($image_name, $image_path))
{
$result = mysql_query($query);
if($result)
{
echo "Image Uploaded Sucessfully";
}else{
echo "Error Inserting Into database";
}
}else{
echo "Error Inserting Image Into File System";
exit;
}
?>

1 Like 1 Share

Re: PHP Code To Insert And Retrieve Images From Mysql Server by yorex2011: 7:19pm On May 18, 2016
Oga use an e-commerce platform.. Prestashop, openCart.. Them plenty grin
Re: PHP Code To Insert And Retrieve Images From Mysql Server by wallmosh: 7:28pm On May 18, 2016
dhtml18:

It is possible, but I will advise you to write your WILL before you embark on it.


There are quite good number of videos on YouTube that walk you through.


check phpacademy series you've your solution
Re: PHP Code To Insert And Retrieve Images From Mysql Server by teampregar(m): 5:47pm On May 19, 2016
See them nairaland programmers, just discouraging someone because they cannot offer help, instead of referring him to an e-commerce web app, while not teach him what those e-commerce apps uses..* this is why stackoverflow i better than nairaland programmers section always discouraging peoples ideas*

Bro,
MySQL supports a data type to store stuffs like images,videos,audios,pdf etc.

it is called BLOB(Binary Large Object)
if u use windows OS just right click on any image on your library and click on edit with notepad, you will see that a simple photo will turn into a very long text comprised with different syntax , letters and numbers*these are BLOBS*

u can create a table like these

CREATE TABLE IF NOT EXISTS images(
id INT UNSIGNED AUTO_INCREMENT,
image_name VARCHAR(any length u like) NOT NULL,
image BLOB NOT NULL,
PRIMARY KEY(id)
)

u can use BLOB, MEDIUMBLOB or LONGBLOB depending on the file size of the files u want to accept

then when u want to output the file to html use the following code
//do mysql conections before the following
$sql = 'SELECT image FROM images ';
$results = //query your mysql server fetch the results as an associative array

//then output to browser with
header('Content-Type://mime-type of ur image');
echo //the image u retrieved

i could not write the full stuff cause i post with a mobile phone mha hand dey pain

bt i advice u store the location of the images instead then on request u link to the location with html img element

this will be faster on a shared host except u are using a dedicated server or a cloud system

1 Like

Re: PHP Code To Insert And Retrieve Images From Mysql Server by Nmeri17: 7:50am On May 20, 2016
yusufabayomio:
Hi, its better to use an existing ecommerce open source tools, than developing from the scratch. You can try:
OpenCart, Magento or WooCommerce (If you are conversant with WordPress)

Sorry, I'm curious: Why do you think it is "better" not to build it himself if he can??
Re: PHP Code To Insert And Retrieve Images From Mysql Server by 2mNaira: 12:55pm On May 21, 2016
jacob05:
This might help: http://www.phpro.org/tutorials/Storing-Images-in-MySQL-with-PHP.html

This tutorial makes a lot of sense.I will apply the knowledge in my next app.
Re: PHP Code To Insert And Retrieve Images From Mysql Server by Nobody: 1:23pm On May 21, 2016
No matter how many people say it, i will never ever agree that blob is the way at all. The day you try to backup your database offline and you have like lets say 1 million images stored inside, you are in deep-shit man, especially with Nigerian internet connections.
Or if you have to do database migration and some other other stuffs. Even your incremental backups will finish up your space in no time.

Because of this, i have a database somewhere online that is several Gigabytes in size, i still dont know what to do with it.

What if you want to generate thumbnails - you will have to read that blob into a file, generate thumbnail, blob the thumbnail and erase the disk file. . . . .just imagine that

And the day the database crashes or get hacked - and imagine it is a social networking website - and the owner calls you, i wonder what you will say - if you were me, you will start with:: weeeeeeell. . . . . . .eeeeeeeeehm, i i i waassss ehm ehm trying to make some sort of incremental backup and the data must be somewhere on the webserver

You guys had better learn from those that have experimented with this method (like me) and have failed. It might look like a good idea at first, but with time, you will blame yourself.

1 Like

(1) (Reply)

Where To Learn Web Application Development In Onitsha:pls Help / Github / I Want To Create A Malware (spyware)

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 28
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.