By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,968 Members | 1,844 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,968 IT Pros & Developers. It's quick & easy.

How can i put the image file to the db ? (mssql)

P: n/a
Hi,

i have a little problem. i hope u guys give me a clear solution (:

db: mssql

i just want to put jpeg file to the image field at the mssql db.

and after that i want to call it back..

how can i do this ?
i tried this code ;
$image = $_FILES['form_data']['name'];
$image_yol = $_FILES['form_data']['tmp_name'];
$newdata = "$image_yol";
$data = addslashes(fread(fopen($image_yol, "r"),
filesize($image_yol)));

to put image to the db...
but it gave me an error like
"'PHP Warning: mssql_query(): message: Line 7: Incorrect syntax near
'Japanese'. (severity 15) in d:\www\docs\php_viva\functions.php on
line 3 PHP Warning: mssql_query(): message: The identifier that starts
with 'vG!kZJ{vUtŸ\\\'_~’z‰›nŽa{W ‹_‘~M\\\"[cŒ$’~j#nˆ-lr‹$s—QҾ^er}_9{UL ‹'
is too long. Maximum length is 128. (severity 15) in
d:\www\docs\php_viva'is too long. Maximum length is 128"

do you have any idea?
Thanks,
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Alper Adatoz" <al****@myway.com> wrote in message
news:ea**************************@posting.google.c om...
Hi,

i have a little problem. i hope u guys give me a clear solution (:

db: mssql

i just want to put jpeg file to the image field at the mssql db.

and after that i want to call it back..

how can i do this ?
i tried this code ;
$image = $_FILES['form_data']['name'];
$image_yol = $_FILES['form_data']['tmp_name'];
$newdata = "$image_yol";
$data = addslashes(fread(fopen($image_yol, "r"),
filesize($image_yol)));

to put image to the db...
but it gave me an error like
"'PHP Warning: mssql_query(): message: Line 7: Incorrect syntax near
'Japanese'. (severity 15) in d:\www\docs\php_viva\functions.php on
line 3 PHP Warning: mssql_query(): message: The identifier that starts
with 'vG!kZJ{vUtŸ\\\'_~’z‰›nŽa
{W‹_‘~M\\\"[cŒ$’~j#nˆ-lr&#8249
;$s—QҾ^er}_9{UL‹' is too long. Maximum length is 128. (severity 15) in
d:\www\docs\php_viva'is too long. Maximum length is 128"

do you have any idea?
Thanks,


Why do you want to fill up a database with this sort of stuff?
Why not just save the file to a directory, it would be much quicker and
won't slow down an already slow MSSQL server.

Regards
Richard Grove
http://shopbuilder.org - ecommerce systems
Become a Shop Builder re-seller:
http://www.affiliatewindow.com/affil...ls.php?mid=611
http://www.affiliatewindow.com/a.pl?590
Jul 17 '05 #2

P: n/a

"Alper Adatoz" <al****@myway.com> wrote in message news:ea**************************@posting.google.c om...
Hi,

i have a little problem. i hope u guys give me a clear solution (:

db: mssql

i just want to put jpeg file to the image field at the mssql db.

and after that i want to call it back..

how can i do this ?

You can do this in PHP/MySQL, but you should develop this in an iterative stepwise fashion so that you understand what is happening.
Your code below has two issues. The first is uploading and saving a file. The second is saving binary date to a MySQL database.

i tried this code ;
$image = $_FILES['form_data']['name'];
$image_yol = $_FILES['form_data']['tmp_name'];
$newdata = "$image_yol";
$data = addslashes(fread(fopen($image_yol, "r"),
filesize($image_yol)));

With out getting too deep here, your first goal is to be able to upload and save a file - any file - graphic or otherwise. Here's a
hint. You are not going to be "addslashes" to the raw data - only to the data coming from the HTML form.

The PHP documentation is excellent and you will probably find some code you can copy and past from here:
http://www.php.net/manual/en/functio...oaded-file.php
to put image to the db...
but it gave me an error like .... do you have any idea?

The next step is saving data to the database. I assume you can save and retrieve text data from the database already. If not,
start there. Once you do that read up on "Blob" data types. The MySQL documentation is not laid out as well as PHP.net (in fact
PHP.net should be the model for document presentation... I digress...) but have fun searching for the information here:
http://www.mysql.com/documentation/m...ter/index.html
Now you can combine what you've learned and you will have your answer.

But as pointed out in another one of your responses, you may be better off saving the graphic to an "images" directory and then
simply saving the URL to the image in your database.

This would be easier on the database and will be easier to retrieve (you won't have to worry about sending HTTP header info when you
want to display the image from the database), but your requirements may not allow you to be this flexible (i.e. copywrite issues
won't allow a static URL to an image).

Hope this helps,

CF
Jul 17 '05 #3

P: n/a
Hi Alper Adatoz,

"Alper Adatoz" <al****@myway.com> schrieb im Newsbeitrag
news:ea**************************@posting.google.c om...
i just want to put jpeg file to the image field at the mssql db.


I've done this only with MySQL before, but I figure it could be similar with
MSSQL.

1. You can create a BLOB field of the appropriate size in the database
table.
2. Then, you can load the image file that has been uploaded by the user
(like, with file_get_contents() ).
3. Then, you encode the binary data to base64 format using base64_encode().
4. Then, you can simply use it in an INSERT or UPDATE SQL command:

$bindata = file_get_contents( $filename );
$b64data = base64_encode( $bindata );
$result = mysql_query( "INSERT INTO MYTBL ... SET IMAGE='$b64data'",
$dblink );

To retrieve the image stored in the database, simply read the row, access
the field, base64_decode() it and output it to the user.

For output to the user, you can use a specific PHP script that reads the
image from the database, sets the HTTP headers and then outputs the binary
data.

See RFC 2616 (HTTP 1.1 specification), available from
http://www.rfc-editor.org, for information about the HTTP protocol.

This method has one disadvantage, that if the base64-encoded image data
would exceed the SQL INSERT/UPDATE command length limit. I haven't tried yet
if databases like MySQL can use arbitary-sized SQL queries.

I hope that helps.

Regards,
Ekkehard Morgenstern.
Jul 17 '05 #4

P: n/a
"Alper Adatoz" a crit le 11/11/2003 :
i just want to put jpeg file to the image field at the mssql db.
and after that i want to call it back..

how can i do this ?


Appart from very very specific issues this is to be avoided!
You would much prefer a directory where you put your files and just the
filename in the database.
MySQL is not optimised for this.
Jul 17 '05 #5

P: n/a
ChronoFish wrote:
"Alper Adatoz" <al****@myway.com> wrote in message news:ea**************************@posting.google.c om...
Hi,

i have a little problem. i hope u guys give me a clear solution (:

db: mssql

i just want to put jpeg file to the image field at the mssql db.

and after that i want to call it back..

how can i do this ?


You can do this in PHP/MySQL, but you should develop this in an iterative stepwise fashion so that you understand what is happening.
Your code below has two issues. The first is uploading and saving a file. The second is saving binary date to a MySQL database.
i tried this code ;
$image = $_FILES['form_data']['name'];
$image_yol = $_FILES['form_data']['tmp_name'];
$newdata = "$image_yol";
$data = addslashes(fread(fopen($image_yol, "r"),
filesize($image_yol)));

With out getting too deep here, your first goal is to be able to upload and save a file - any file - graphic or otherwise. Here's a
hint. You are not going to be "addslashes" to the raw data - only to the data coming from the HTML form.

The PHP documentation is excellent and you will probably find some code you can copy and past from here:
http://www.php.net/manual/en/functio...oaded-file.php

to put image to the db...
but it gave me an error like


...
do you have any idea?


The next step is saving data to the database. I assume you can save and retrieve text data from the database already. If not,
start there. Once you do that read up on "Blob" data types. The MySQL documentation is not laid out as well as PHP.net (in fact
PHP.net should be the model for document presentation... I digress...) but have fun searching for the information here:
http://www.mysql.com/documentation/m...ter/index.html
Now you can combine what you've learned and you will have your answer.

But as pointed out in another one of your responses, you may be better off saving the graphic to an "images" directory and then
simply saving the URL to the image in your database.

This would be easier on the database and will be easier to retrieve (you won't have to worry about sending HTTP header info when you
want to display the image from the database), but your requirements may not allow you to be this flexible (i.e. copywrite issues
won't allow a static URL to an image).

Hope this helps,

CF


i am using php and mysql on my localhost

Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.