Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Alper Adatoz
Guest
 
Posts: n/a
#1: Jul 17 '05
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,

Richard Grove
Guest
 
Posts: n/a
#2: Jul 17 '05

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


"Alper Adatoz" <allper@myway.com> wrote in message
news:eae08b72.0311110522.62b12fd3@posting.google.c om...[color=blue]
> 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[/color]
'vG!kZJ{vUtŸ\\\'_~’z‰›nŽa
{W‹_‘~M\\\"[cŒ$’~j#nˆ-lr&#8249
;$s—QҾ^er}_9{UL‹'[color=blue]
> 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,[/color]



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


ChronoFish
Guest
 
Posts: n/a
#3: Jul 17 '05

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



"Alper Adatoz" <allper@myway.com> wrote in message news:eae08b72.0311110522.62b12fd3@posting.google.c om...[color=blue]
> 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 ?[/color]


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.

[color=blue]
> 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)));
>[/color]

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


[color=blue]
> to put image to the db...
> but it gave me an error like[/color]
....[color=blue]
> do you have any idea?
>[/color]


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


Ekkehard Morgenstern
Guest
 
Posts: n/a
#4: Jul 17 '05

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


Hi Alper Adatoz,

"Alper Adatoz" <allper@myway.com> schrieb im Newsbeitrag
news:eae08b72.0311110522.62b12fd3@posting.google.c om...[color=blue]
> i just want to put jpeg file to the image field at the mssql db.[/color]

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.


Jedi121
Guest
 
Posts: n/a
#5: Jul 17 '05

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


"Alper Adatoz" a crit le 11/11/2003 :[color=blue]
> 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 ?[/color]

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.


pc_newbie
Guest
 
Posts: n/a
#6: Jul 17 '05

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


ChronoFish wrote:
[color=blue]
> "Alper Adatoz" <allper@myway.com> wrote in message news:eae08b72.0311110522.62b12fd3@posting.google.c om...
>[color=green]
>>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 ?[/color]
>
>
>
> 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.
>
>
>[color=green]
>>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)));
>>[/color]
>
>
> 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
>
>
>
>[color=green]
>>to put image to the db...
>>but it gave me an error like[/color]
>
> ...
>[color=green]
>>do you have any idea?
>>[/color]
>
>
>
> 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
>
>[/color]

i am using php and mysql on my localhost

Closed Thread


Similar PHP bytes