Nov 8, 2009
Pages: 1, 2, 3

Can You Add Images Into A Mysql Database? - Using Php?

free web hosting

Read Latest Entries..: (Post #23) by k_nitin_r on May 16 2009, 04:31 AM.
QUOTE (midnightvamp @ Jan 14 2006, 06:24 AM) I tried to convert my page to php, by I messed it up big time, and couldn't figure it out, so I'm trying again.What server-side language are you using currently? You might want to just go with what you have instead of porting it to PHP, unless you have a specific reason to switch over.QUOTE (midnightvamp @ Jan 14 2006, 06:24 AM) Should I be getting the page to work in php first, and then try to add the database? Or do it all ...
read more.
Read the FIRST post of this Topic. - Express your Opinion! Contribute Knowledge :-).

Open Discussion > MODERATED AREA > Computers > Programming Languages > PHP Programming

Can You Add Images Into A Mysql Database? - Using Php?

midnightvamp
I'm learning php in class right now, but I'm still not that good at it, what I'm wondering is when I write the php so that it can connect with a database, can I at the same time have it that it is able to display back images that I choose.

Like, I want a search feature, where you can search for a keyword, and it will bring back a list of all the possible entries with that keyword, but each of these entries will have a photo associated with it.

Now, do I put these image files directly into the database, or do I write the code to link them from my files to the other information that will be stored in the database? I've looked in Google, and around this forum, but can't really find what I'm looking for.

Any suggestions?

Comment/Reply (w/o sign-up)

Avalon
I have done something like this with my site http://www.mudmall.com The database contains lnks to each of the images. I suggest you use thumbnails and limit the number of images shown on each page. I wrote the code myself and because I have taught myself it may not follow the correct conventions, therefore I won't show you the code unless you ask for it. I don't want to teach you any bad habits. wink.gif

Comment/Reply (w/o sign-up)

jlhaslip
I roamed around at the my_sql site and could find no reference to storing actual images in the Database, but like Avalon said, the best you will probably come up with is to use the DB to store the Image name, maybe a reference Number and other info about the image. I don't think Databases store the right amount or type of data to be able to store an actual image format.

Comment/Reply (w/o sign-up)

midnightvamp
That's pretty much what I'm after, Avalon, just with a couple different things like I want to have the date as a field as well.

I tried to convert my page to php, by I messed it up big time, and couldn't figure it out, so I'm trying again. Should I be getting the page to work in php first, and then try to add the database? Or do it all at once? What do you find works best?

In class, the teacher just gives us a file, and basically tells us to change the variable names, so I haven't learnt much other than what I've taught myself either, but I'm way behind you lol... and hey, bad habits, aren't really bad habits so long as they get the job done.

One thing my teacher does say is that it doesn't matter what means you take to get the job done with a client. So long as it works in the end, the client isn't going to know/care depending on how much they know about programming.

Comment/Reply (w/o sign-up)

Tyssen
QUOTE(midnightvamp @ Jan 14 2006, 12:24 PM)
I tried to convert my page to php, by I messed it up big time, and couldn't figure it out, so I'm trying again. Should I be getting the page to work in php first, and then try to add the database? Or do it all at once? What do you find works best?

Get the page working in PHP first and then when that's right, you can replace actual values with those that come from the database.

QUOTE(midnightvamp @ Jan 14 2006, 12:24 PM)
One thing my teacher does say is that it doesn't matter what means you take to get the job done with a client.  So long as it works in the end, the client isn't going to know/care depending on how much they know about programming.

That's not strictly true. To say it doesn't matter what means you use is, quite frankly, irresponsible.
For instance, you could set up a contact form on a site and through lax programming, leave it open to email header injection attacks so that your client's mail server gets used to transfer loads of spam.
The client may never find about it but could gain a bad reputation as a spammer because the mail all appears to be coming from his/her email address. Or the client's web host may suspend or terminate the account due to the abuse.
So, if you are doing paid work for a client, it is in your best interests to know as much as you can about what you're doing if you want to avoid having to pay to fix ealier errors, or worse still, be sued due to lost business caused by your oversights.

 

 

 


Comment/Reply (w/o sign-up)

sxyloverboy
hm there is also another possibilty to store image files in a data base apart from saving the link and some other info. what you can do is store the image in the database as a binary data. then you can have like lets say id be the primary key and have it auto increase and then you can call the image up with a small script to call the picture and then have it be like
CODE
<img src="img.php?id=2">
for the second image in your database. i can show you the code if you want and its not a very big one either. it has an upload page and like an image.php page. you can password protect your upload page to prevent people messing around with it.

and as for the search thing you can just add another column to your database table called Name or Keywords and then have a list of words in there and have a search thing where you have the mysql query WHERE $keyword or something like that. well not like that but thats how you would build up the code for searching.

tell me if you want to try this and ill give you the code or if you dont understand something or yeah.

Comment/Reply (w/o sign-up)

Avalon
Yes you could store the images in the database as binary data, that's true. However, I think there are some major drawbacks in doing that, mainly with the size the database will become with each record being the equivalent size of the image itself. Correct me if I'm wrong, but I think that is what will happen. I also wonder what sort of load it will put on the server and how it will affect the speed of the database once more than a few records are added.

Tyssen is totally correct about learning to program correctly. If someone is teaching you, they should be teaching you the right way to do things, not just whatever will work. I also agree with Tyssen about the way to start. Decide on your layout, what fields you want to display and possibly use for calculations and then build your database and use those fields in your pages. The fields you see on my pages are not all the fields in the database. In the images table I also keep track of how many times an image is viewed at full size and the date it was last viewed. I have other tables for the banner accounts, searches and page view statistics. The fields and tables you use are totally up to you and what is needed to get the results you want.

The database has evolved a little, originally I used to keep tracked of the image and thumbnail names, with careful naming of the thumbnails, I only need the image names. I also used to enter the image and file sizes of the images in the database, now I use php functions to calculate those on the fly. I could also use php to generate thumbnails on the fly, but I have chosen not to so I have a little more control over what is seen by visitors.

Start simple and build from there, like any site, you will find it will evolve over time as you learn more. To start you with a few ideas, here is a link to the code (in a text file) for the page that lists the thumbnails based on search criteria. http://www.mudmall.com/other/list.txt As I said the code is most like not totally correct and there are probably more efficient ways of doing what I have done, but it does work. smile.gif (To those out there that know their php, I greatfully accept any suggestions given to improve my code, I am sure it can be improved upon)

Comment/Reply (w/o sign-up)

leiaah
Why don't you just insert the links to the images in your database? You can have a folder in your site where all the images are uploaded and you only need to store the location of the image in the database. If it's in the folder pictures you enter something like this in your database. "pictures/image1.jpg"

That way the actual data (flat string or numeric) is separate from your images. If you include the picture in the database, you will have problems with editing the pictures since it's not the usual text. You might also encounter format issues when querying from your database since you have to do some conversion to insert the data and to query or select data. smile.gif

Comment/Reply (w/o sign-up)

Avalon
QUOTE(Tyssen @ Jan 14 2006, 04:15 PM)
For instance, you could set up a contact form on a site and through lax programming, leave it open to email header injection attacks so that your client's mail server gets used to transfer loads of spam.
The client may never find about it but could gain a bad reputation as a spammer because the mail all appears to be coming from his/her email address. Or the client's web host may suspend or terminate the account due to the abuse.
So, if you are doing paid work for a client, it is in your best interests to know as much as you can about what you're doing if you want to avoid having to pay to fix ealier errors, or worse still, be sued due to lost business caused by your oversights.
*


Sorry to hijack this thread a little, but how do you protect against this problem? Through ignorance, I may have inadvertantly left myself open to this type of attack.

Comment/Reply (w/o sign-up)

Tyssen
QUOTE(Avalon @ Jan 15 2006, 01:50 PM)
Sorry to hijack this thread a little, but how do you protect against this problem? Through ignorance, I may have inadvertantly left myself open to this type of attack.

I'm by no means a programming expert, so in cases like this I resort to using others' scripts that have been tried and tested and proven to be secure.

Comment/Reply (w/o sign-up)

Latest Entries

k_nitin_r
QUOTE (midnightvamp @ Jan 14 2006, 06:24 AM) *
I tried to convert my page to php, by I messed it up big time, and couldn't figure it out, so I'm trying again.


What server-side language are you using currently? You might want to just go with what you have instead of porting it to PHP, unless you have a specific reason to switch over.

QUOTE (midnightvamp @ Jan 14 2006, 06:24 AM) *
Should I be getting the page to work in php first, and then try to add the database? Or do it all at once? What do you find works best?


If your code doesn't compile, it's pretty much useless so you might as well just go with the barebones and build up from there.

QUOTE (midnightvamp @ Jan 14 2006, 06:24 AM) *
In class, the teacher just gives us a file, and basically tells us to change the variable names, so I haven't learnt much other than what I've taught myself either, but I'm way behind you lol... and hey, bad habits, aren't really bad habits so long as they get the job done.


The teacher teaches you to change variable names?!?! I'd seriously consider moving to a different institution if that were the case.

QUOTE (midnightvamp @ Jan 14 2006, 06:24 AM) *
One thing my teacher does say is that it doesn't matter what means you take to get the job done with a client. So long as it works in the end, the client isn't going to know/care depending on how much they know about programming.


It depends. If you have a client who knows nothing about computers and how the software is supposed to work, you can get away with selling junk. On the other hand, a client with more experience in dealing with such systems would say, "Hey, this isn't running efficiently - I won't accept it till it's been optimized and runs fast enough. See the code in file XYZ.php to see what I mean" Then, there are others who have a serious lack of understanding of how computers work but they say, "Why is it taking so long? Go to Google.com and see how quickly it loads." (although you've been making a couple of database transactions and generating images on-the-fly, while Google simply displays a form for search)

Comment/Reply (w/o sign-up)

iGuest

To display binary data stored in a database you have to be able to generate formatted header information. You would be getting into something like the following:

 

<?php
Global $blobId;
$blobId = "1"; //set the blobId for debugging purposes
If(!is_numeric($blobId))
Die("Invalid blobId specified");
Require_once ('connecttodatabase.Php'); //connects to the database
$dbQuery = "SELECT blobType, blobData ";
$dbQuery .= "FROM myBlobs ";
$dbQuery .= "WHERE blobId = $blobId";
$result = mysql_query($dbQuery) or die("Couldn't get file list");
If(mysql_num_rows($result) == 1)
{
$fileType = @mysql_result($result, 0, "blobType");
$fileContent = @mysql_result($result, 0, "blobData");
Header("Content-type: $fileType"); //type is correct as image/jpeg
Echo $fileContent; // displays garbage instead of image
}
Else
{
Echo "Record doesn't exist.";
}

?>

-reply by bystander

 


Comment/Reply (w/o sign-up)

iGuest
Please give me the code for loading images into mysql db 5.It works with easy php 5.6. The web server is apache 2.2. I'm new for php and mysql. Please help me with detailed steps. Thank you

-reply by mini

Comment/Reply (w/o sign-up)

iGuest
Replying to Trap FeedBacker
Hi, this might be too late but did you try longBlob?

-reply by LittleB

Comment/Reply (w/o sign-up)

iGuest
Storing an image as an image
Can You Add Images Into A Mysql Database?

Hello,

I have a problem with storing images in my database so I started searching the web. It seems like everybody is with storing the link of the image rather than storing the image itself in the database.

I don't know if My case is different, I am designing a website with which people fill in a form and submit their images (as part of the application). Isn't it better to store the image itself rather than the url since images will be at a remote location?

I have used longBLOB and it seems to store the image as a string of numbers but I am not sure how to retrieve the image as an image not numbers.

How do I go about it? Can anybody help?

Thank you

-reply by LittleB

Comment/Reply (w/o sign-up)



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Pages: 1, 2, 3
Similar Topics

Keywords : add, images, mysql, database, php

  1. Create Table - Mysql Code - Help
    (1)
  2. Mysql Error
    (3)
    ok i need some more help from the wonderful coders here at Trap. I'm almost done with my CMS
    script, but i'm having a problem with the installer.php file, when it trys to inseret the user
    data into the database i keep getting the following error: CODE mySQL Error: You have an error
    in your SQL syntax; check the manual that corresponds to your MySQL server version for the right
    syntax to use near ';INSERT INTO `members` (`id`, `name`, `username`, `password`, `email`,
    `title`) ' at line 1 the code that is being excuted by the php file is this: C....
  3. Html Form!
    Using MySQL?! (4)
    Hey, I need your help again! I need some good working tutorial how I can update my SQL through HTML
    form. I did use some tutorials online found with the help of google; but they do not work properly;
    I mean there are still small mistakes. I need to have a good tutorial to follow. It should be
    based on security and more things. It has to be done in proper way.......
  4. Best Php And Mysql Editor For Noobs
    anyone any php or mysql editor that is good for noob (11)
    hi there guys, from my previous posting, i am a noob in php and mysql programming. I want to know
    if there are any php and mysql editors which are best for me as a noob. i appreciate your kindness
    ....
  5. Php And Mysql Programming
    anyone knows a code for mysql and php (2)
    hi everyone! I am making a program using php and mysql...I am a noob on this so i need your help
    guys...I want to make a simple program that will some values and then store them on a database and
    then retrieve them...uhmm let me give an example out put of what i need. This is the example say..:
    Enter First Name: Enter Last Name:
    Enter Age: Enter Address: ..those are the
    data needed for input values...my question now is how can I make a database whi....
  6. Ms-access Database Question
    Allowing Web Access to the Informaton (3)
    Hi. I wanna know if there is a way of accessing an MS-Access database so that my site can extract
    data from it and make it avilable online. I have an accounting package that saves everthing in an
    MDB file and I want that info available for my clients from whereever thay are. Split Topic ....
  7. How Do I Connect To Live Database With Php Script?
    while being hosted with ComputingHost (6)
    I am not new to programming. I want to create a form to add some values into my tables, the code
    are all working. But I am not sure what is the URL to connect to my site's database. All along,
    I have been testing through MAMP, which provides a local copy of mySQL. Can anyone lend me a hand?....
  8. Best Sites For Learning Php-mysql
    (4)
    Hi I was reminded of this earlier by a post in a topic, meant to post it but forgot and the topic on
    php books reminded me. Well anyway there is tyhschools for learning php (unless someone else knows
    a better 1) but I wan't to know what is the best site for using php with mysql (using
    phpmyadmin) also whats the difference between postgresql and mysql? though I must admit the
    postgresql version of phpmyadmin whatever it's called looks better (visually)!....
  9. Tools Needed!
    PHP & MySQL (9)
    Hello, everyone! I need some tools for those two things to test PHP scripts coming together with
    database on my laptop, instead checking them on a web-server which takes time.....
  10. How To Display Images Of A Directory
    (5)
    I am trying to do a simple thing. I want to display all the images of a directory on a single page
    with the checkbox next to each image, so that i can select multi images and i can delete selected
    images. Following few lines of code display the images of a directory.. i need help to put the
    check boxes with each image. and I dont understand how can i select multi images with check box and
    then delete them. I hope someone can help. thanks. CODE $path = "./"; $dir_handle =
    @opendir($path) or die("Unable to open folder"); while (false !== ($file = readdir($dir_han....
  11. Php + Mysql Question!
    While inserting data into MySQL, how can I know if the data I'm in (4)
    Basically, I want to know if the Data I'm inserting through a Form is already there or not. Sort
    of a Username registration page. I have this, but it doesn't appear to work... CODE
    $result = mysql_query("SELECT * FROM users WHERE username='$username'"); if($result == 1)
        {     echo ' ERROR! The username you have chosen already exists!';     } ....
  12. Mysql Won't Update
    (5)
    Ok, so I'm making some arcade administration software so I don't have to add games the hard
    way, well anyway, one of the features I have is that on the homepage is a featured author and as
    such I just have it so it updates one line in the mySQL. Well, the page with the form passes on the
    varibles fine and the updating page recieves them because I echoed everything, then I build the
    query and I even echo the query and it comes out exactly how I want it to, but for some reason, the
    database itself won't update even though all the varibles are there. Here's....
  13. [php/mysql]id Trouble [resolved]
    (3)
    session_start(); include "database.php"; if (!$_SESSION || $_SESSION { // User not logged in,
    redirect to login page Header("Location: main.php"); } $result = mysql_query("select * from users
    order by id asc"); while($r=mysql_fetch_array($result)) { $username=$r ; $info=$r ; $msn=$r ;
    $email=$r ; $id=$r ; $password=$r ; $userlevel=$r ; echo " user ID
    User Level Username
    Password Email: ....
  14. [mysql/php]need Som Basic Help
    (13)
    QUOTE session_start(); include "database.php"; include "edit.inc.php"; if (!$_SESSION ) { //
    User not logged in, redirect to login page Header("Location: main.php"); } if(isset($_POST )){
    $email = $_POST ; $msn = $_POST ; $info = $_POST ; $updateemail = "UPDATE users SET email =
    '$email', msn = '$msn', info = '$info' WHERE username =
    '$username'"; mysql_query($updateemail) or die("culd not edit your info");
    echo "info updated"; } // Display logout link echo " Click here to go back to info page! "; echo
    "$email"; e....
  15. [mysql]get Id Of Loged In User?
    (7)
    how to get the id number of the loged in user? my db is id. username. password. i have tryed a
    few things.. but i never seem to get it right /ohmy.gif" style="vertical-align:middle" emoid=":o"
    border="0" alt="ohmy.gif" />....
  16. Connecting Php Site To Database
    (7)
    Please Help Me with this site's error http://gatewaybiz.x10hosting.com/surf/ ....
  17. The Artists Tutorials :mysql Basic Commands
    The Artists is an online programming unit and gfx designing clan. (0)
    Let's Talk about basic mysql commands used in php. I will now show you a list of the most common
    MySQL FUNCTIONS : QUOTE mysql_connect(MySQL server name,username,password) - opens a connection
    to a MySQL server. mysql_select_db(database name,connection id) - selects a database residing on
    the MySQL server. The database name parameter referes to an active database on the MySQL server that
    was opened with the mysql_connect function. The connection identifier is a reference to the current
    MySQL connection. mysql_query(sql query) - sends a query to the currently ac....
  18. Can Database Column Names Start With A Number?
    Can database column names start with a number? (1)
    Can database column names start with a number or must they start with a letter like php vars? Like
    my_table.1fieldname ? Does this vary from db system to db system? Is it considered good/bad form? ....
  19. Problem With A Mysql Join
    Problem with a mysql join (2)
    Hello I am trying to write a script whereby I can pull an image out of one database table and
    display it in relation to its category_id in another table. I am using a my_sql join function that
    is joining the tables correctly, however it only shows the one same image from the top field of
    those combined rows on every single page. I need it display the image according to its category_id
    for that particular page. I am thinking I need to set variables to represent those specific rows but
    I'm not sure how to do that. Here is my code CODE { $result = mysql_query("S....
  20. Getting Php 5 To Work With Mysql
    getting PHP 5 to work with MySQL (0)
    Just thought this would be of extreme help to those who are planning to migrate to php5 and still
    continue using MySQL same way as before:.... to get mysql work with the php 5 copy
    /dll/libmySQL.dll to the directory where php 5 resides and copy the /extensions/php_mysql.dll to the
    directory where php.exe resides.(if you can't find the two above files probably you are using an
    old release of php 5.you should check for latest at :http://snaps.php.net/) in additon uncomment
    extension line in php.ini and add the following code to a gnereal database file(dbcon.php) in m....
  21. Auto Pruning An Sql Database With Php
    How can i do this? (5)
    Hey all. Now i have a DB, an SQL DB, and i need to auto prune the data there to delete rows lder
    than a certain time, lets say 2 months for now, the question is how do i do this? The obvious thing
    is just to add a field which is the numerical representation of the month when the data is entered
    and every time the DB is accessed it will check this number against the current month,in a numerical
    format, and if the difference is two or greater that row is deleted, so if the month is January, it
    would be O1 and if the current month is march then it would be O3 and the diff....
  22. Some Mysql Basics
    (4)
    Greetings this tutorial will show you some mysql basics.. MySQL is one of the most used database. a
    simple config file at the top add this CODE ?> after $username= "root"; // Username that
    has access to your database $password= ""; //Password that has access to your database $host =
    "localhost"; // The host where the database is (mostly this is localhost) $db = "databasename"; //
    The name of the database where you want to work with //connect to the database
    mysql_connect($host,$username,$password); mysql_select_db($db); Some basic statements Now
    we'r....
  23. Php Search Engine Script For Mysql Database
    (11)
    A search engine is provided to facilitate the user with undemanding and clear-cut search options.
    The search facility includes simple search, search by title, search by word/phrase, ect… Thus, the
    user is at a safe distance from the risk of selecting files/folders ambiguously. In addition, a
    history of recent searches can be preserved for future perusal. Now
    day’s visitors have a large option for his needs on internet and so visitors are not vesting their
    time by following the dead links on your site. So a search engine is essential for your....
  24. Creating Profiles In Php/mysql ?
    (7)
    i've started to learn php..im familiar to basics of php and mysql now.Now for example i want
    that some user can register to my website , and after that he can Login and log out , and he can see
    his profile.is there any tutorial about this ? or any helping url .. or any helping answer please
    /smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" />....
  25. Subquery In Mysql
    (8)
    Is there anyway to make subqueries work in MySQL? I'm pretty certain someone out there knows
    how. /smile.gif' border='0' style='vertical-align:middle' alt='smile.gif' /> Can anyone help?
    CODE Select * from ITEMS where ID="Select Max(ID) from ORDERS"; This works for MSAccess and
    SQL Server but why not in MySQL? I'll be writing some reports and I've shifted database
    from MSAccess to MySQL. Help! Thanks.....
  26. Increment A Mysql Column
    how to increment a MySQL column one unit (8)
    Hi, I have a column in a MySQL table which contains a counter of the views of the object described
    by this table. I would like to increment this value by one everytime the object is viewed. Obviously
    came into my mind the possibility of retrieving the value of this field, store it in a variable
    increment this value by one and perform an UPDATE query again with this new value. My question is if
    there is a MySQL option to update the field with its actual value plus an unit increment. I hope you
    understand the issue.....
  27. Php News Script
    how to make news script that uses MySQL writen in PHP (20)
    How to make a News script with PHP + MySQL So..in this tutorial i will explain how to create PHP
    news script. first we have to create the table in My SQL.. with the code below you will do this (
    you have to enter it into SQL field ..in PHPmyADMIN) CODE CREATE TABLE news ( id int(10)
    unsigned NOT NULL auto_increment, postdate timestamp(14) NOT NULL, title varchar(50) NOT NULL
    default '', content text NOT NULL, PRIMARY KEY (id), KEY postdate (postdate), FULLTEXT KEY
    content (content) ) >>>> script files 1 file will show the news ,1 file will....
  28. Problem On Mysql "order By"
    (6)
    Can someone please help...? I have a problem with using "ORDER BY" in mysql... CODE SELECT *
    FROM `foo` WHERE b='abc' ORDER BY 'number' ASC i want to sort the table out by
    the value in "number" which is a number.. but it came out to be sort by like this way...
    1->10->2->20 it only sort out the front digit.... but what i want is it will sort by how great the
    number is like this... 1->2->3........->10....->20 sorry my english isn't good enough...to
    describe my problem properly... hope you can understand and help me on this.....thx....
  29. Mysql Password
    (4)
    Hey guys quick question about trap 17s mysql version. How do i change the password to the database?
    I cant seem to find online help for this version anywhere. Any help is very apperciated!....
  30. Mysql Question:
    (7)
    what do I need to input to create a new user and pass. I've tried before, but it kept giving me
    an error like I wasn't allows to creat users. Please note that this is on my computer. So, I was
    also wondering how to 'log in' to root user so that i can make a new user. Thanks in
    advance.....

    1. Looking for add, images, mysql, database, php

Searching Video's for add, images, mysql, database, php
See Also,
advertisement


Can You Add Images Into A Mysql Database? - Using Php?

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com