How to insert image data into SQL Server with php?
Results 1 to 6 of 6

Thread: How to insert image data into SQL Server with php?

  1. #1
    Junior Member
    Join Date
    Nov 2003
    Location
    China
    Posts
    3

    Question How to insert image data into SQL Server with php?

    I come from China.Please give me a sample codes.Thanks!!
    I have a problem.
    I create a image datatype in SQL Server,the name is img.
    The code like these:

    PHP Code:
    $ptemp=$_FILES["myPic"]["tmp_name"];
    $imgLocation str_replace("\\","/"$ptemp);
    $filehandle=fopen($imgLocation,"rb") or die("Can't open file!");
    $sqlPic=addslashes(fread($filehandle,$psize));
    fclose($filehandle);
    $strsql="insert into mytable (img) values ('".$sqlPic."')";
    //...
    mssql_query($strsql);
    //... 
    But no success!! Please help me. Thanks.
    The note like this:
    Warning: mssql_query(): Query failed ......
    Last edited by white_ice; 11-14-2003 at 05:25 AM.

  2. #2
    Junior Member
    Join Date
    Nov 2003
    Location
    China
    Posts
    3

    Smile Help me Help me


  3. #3
    Junior Member CarpeDiem's Avatar
    Join Date
    Sep 2003
    Location
    Eregli, Turkey
    Posts
    11
    maybe you need litle change :-)
    change this
    $imgLocation = str_replace("\","/", $ptemp);
    as
    PHP Code:
    $imgLocation str_replace("\\","/"$ptemp); 
    or

    PHP Code:
    $oldChar=chr(92); 
    $imgLocation str_replace($oldChar,"/"$ptemp); 
    //ThiNkGloBaL&workLocaL
    //metinErGokTas

  4. #4
    Junior Member
    Join Date
    Nov 2003
    Location
    China
    Posts
    3
    PHP Code:
    $imgLocation str_replace("\\","/"$ptemp); 
    I open this file no problem.
    But the SQL Server is error. I think may be the SQL sentence is error.Please help me. Would you give me a sample code?Thank you.

  5. #5
    Senior Member
    Join Date
    Sep 2003
    Posts
    273
    look at your first post...
    see how most of your code is red??/
    this is because you missed a " somewhere and it's reading that whole thing as part of that
    $imgLocation variable...
    why?? Because \ is a note to PHP to ignore the next character and to parse it as if it were a regular character.. Solution?
    PHP Code:
    $imgLocation str_replace("\\\","/", $ptemp); 
    Should work.... The first \ says... dont use PHP to parse the next character... and the second \ is the actual \ that it's replacing.. They way you had it was telling it to ignore the " after your \.

    Hope that helps

    edit:
    on second thought... you might need three... i tried posting it with 2, and it came up with only one on the board, so i added three and it took it right.. maybe it is just a problem with the board display though... But i think it is sposed to be 2, not 3.. try both tho :-p

  6. #6
    Missing in Action LordShryku's Avatar
    Join Date
    Aug 2002
    Location
    Occupational Hypnotherapy
    Posts
    7,227
    Two will work. The board strips slashes

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •