Web Analytics Made Easy -
StatCounter mySQL Blob images help - CodingForum

Announcement

Collapse
No announcement yet.

mySQL Blob images help

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • mySQL Blob images help

    Hey all, i am trying to figure out how to go about loading some images from my database. I have been able to accomplish this with only one image, however. I am looking to loop thought the database and display all images that the query looks for..

    Here is my current code:
    Code:
    Pic.asp page
    
    <%
    Dim oConnection
    Dim oRecordset
    Dim Sql  
      	
    	Response.Expires = -10
    	Response.Buffer = True
    	Response.Clear
    
      	Set oConnection = Server.CreateObject("ADODB.Connection")
    	Set oRecordset = Server.CreateObject("ADODB.Recordset")
    	
    	oConnection.Open "DRIVER={MySQL ODBC 3.51 Driver}; SERVER=nextbowluser.com; PORT=3306; DATABASE=xxxxx; USER=xxxx; PASSWORD=xxxxxxx; OPTION=3;"
    	Sql="select CoupPicVoid from coupbook"
    	oRecordset.Open Sql, oConnection,3,3
      
     	do until oRecordset.eof
    		Response.ContentType = "image/jpeg"
    		Response.BinaryWrite oRecordset("CoupPicVoid")
    		oRecordset.movenext
    	loop
      
      	oRecordset.Close
    	oConnection.Close
    	Set oRecordset = Nothing
    	Set oConnection = Nothing
    %>
    Code:
    testpic.asp
    
    <%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    <img src="pic.asp">
    </body>
    </html>
    As you can tell, the <img src="pic.asp"> displays the image that the database is finding. But it only displays one pic. What can i do in order to display all the images that the query finds?


    Thanks for your time,
    David

  • #2
    I have to say I've rarely seen keeping image data in a database as a good idea. It's almost always easier to store your images in the server filesystem and keep the path references in the database.

    But anyway - you need to do your loop in testpic.asp, not in pic.asp

    I assume you've got some sort of unique ID field in your coupbook table?

    You just need to loop through that table, writing out an image tag that calls pic.asp?imageid=whatever

    Then your pic.asp will return the image binary data for that image id.

    Comment


    • #3
      Ah, OK. That's exactly how i ended up doing it.

      And the reason behind having the images in the database is purely for security reasons. I don't want the user to be able to type in a URL in order to see/get a picture. If its coming from the database then they are unable to do such a thing.

      Thanks again,
      David

      Comment


      • #4
        You *CAN* put the images into files. Just put the files OUTSIDE the web-accessible directories.

        Yes, you then have to use ADODB.Stream to deliver the files, very similar to the DB-based code, but now you do have security.
        Be yourself. No one else is as qualified.

        Comment


        • #5
          Mind showing me an example of this, Old Pedant?

          David

          Comment


          • #6
            Code:
            <%
            Set strm = Server.CreateObject("ADODB.Stream")
            strm.Type = 1 ' binary
            objStream.Open
            objStream.loadFromFile "c:\some\non\web\directory\file.jpg"
            
            Response.ContentType = "image/jpeg"
            Response.BinaryWrite strm.Read()
            
            strm.Close
            Response.End ' to ensure no junk sent
            %>
            Naturally, you can pass in the image name or an id or whatever instead of hard-coding as I have here.
            Be yourself. No one else is as qualified.

            Comment

            Working...
            X