Can't display image from mySQL database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • alexseow
    New Member
    • Dec 2007
    • 6

    Can't display image from mySQL database

    Query.asp
    [code=asp]
    <%@ LANGUAGE="VBSCR IPT" %>
    <!-- #include file="../../includes/dbconn.asp"-->
    <%
    dim MyRs, sqlstr, MyConn

    Response.Expire s = 0
    Response.Buffer = TRUE
    Response.Clear
    Response.Conten tType = "image/gif"

    Set MyConn = Server.CreateOb ject("ADODB.Con nection")
    MyConn.Open dbConn
    Set MyRs = Server.CreateOb ject("ADODB.Rec ordset")

    sqlstr="select img from img"
    MyRs.Open sqlstr, MyConn, adOpenKeyset, adLockOptimisti c
    Response.Binary Write MyRs("img")
    Response.End

    MyRs.Close
    MyConn.Close

    set MyRs=nothing
    set MyConn= nothing
    %>[/code]
    Main.asp
    [code=html]
    <HTML>
    <HEAD><TITLE>Di splay Image</TITLE></HEAD>
    <BODY>
    This page will display the image New Moon Books from a SQL Server
    image field.<BR>
    <IMG SRC="Query.ASP" >
    </BODY>
    </HTML>[/code]

    why can't display, really dunno what to do
    Last edited by JamieHowarth0; Dec 2 '07, 02:06 AM. Reason: Added code tags
  • JamieHowarth0
    Recognized Expert Contributor
    • May 2007
    • 537

    #2
    Hi Alex,

    Try changing the SQL query as follows:
    [code=sql]SELECT [img].[img] FROM [img];[/code]

    SQL does not like parent and child objects (the child/ren field/s of the parent table) to have identical names as it can confuse things.
    The square brackets should act as field name delimiters so the first lot of text in square brackets identifies the table name, followed by the field name.

    Failing that, rename the image binary data field to something different, for example ImgData and let us know how you get on.

    You also were calling your SQL statement twice - using the Open command with a SQL string as a parameter automatically executes the SQL and returns the results. I removed the line where you used MyRs.Execute.

    And finally, simple question - but do you have any image data stored in your database at present?

    Best regards,

    medicineworker

    Comment

    • alexseow
      New Member
      • Dec 2007
      • 6

      #3
      Originally posted by medicineworker
      Hi Alex,

      Try changing the SQL query as follows:
      [code=sql]SELECT [img].[img] FROM [img];[/code]

      SQL does not like parent and child objects (the child/ren field/s of the parent table) to have identical names as it can confuse things.
      The square brackets should act as field name delimiters so the first lot of text in square brackets identifies the table name, followed by the field name.

      Failing that, rename the image binary data field to something different, for example ImgData and let us know how you get on.

      You also were calling your SQL statement twice - using the Open command with a SQL string as a parameter automatically executes the SQL and returns the results. I removed the line where you used MyRs.Execute.

      And finally, simple question - but do you have any image data stored in your database at present?

      Best regards,

      medicineworker

      I have fields with named id (int),img (BLOB) in my table called img.It is correct? and I manually insert a picture through the MySQL yog(db administration interface)

      Comment

      • JamieHowarth0
        Recognized Expert Contributor
        • May 2007
        • 537

        #4
        Hi Alex
        Originally posted by alexseow
        I have fields with named id (int),img (BLOB) in my table called img.It is correct?
        As mentioned previously, add the square braces and the full-stop, and rename the "img" field to ensure that MySQL is reading the query correctly, otherwise it will be interpreted as "SELECT myTable FROM myTable" - as we all know you can't select a table from a table, so by definition it's not correct.

        medicineworker

        Comment

        • pbmods
          Recognized Expert Expert
          • Apr 2007
          • 5821

          #5
          Heya, Alex.

          The exciting part of computer programming is getting a machine that does exactly what you tell it to do... to do what you want it to do.

          You set up your table correctly, and your query looks right... but the server is misinterpreting it. Try adding the proper delimiters to eliminate the possibility of ambiguity in your query.

          Comment

          • alexseow
            New Member
            • Dec 2007
            • 6

            #6
            Originally posted by medicineworker
            Hi Alex

            As mentioned previously, add the square braces and the full-stop, and rename the "img" field to ensure that MySQL is reading the query correctly, otherwise it will be interpreted as "SELECT myTable FROM myTable" - as we all know you can't select a table from a table, so by definition it's not correct.

            medicineworker
            Display.asp
            [code=html]
            <HTML>
            <HEAD><TITLE>Di splay Image</TITLE></HEAD>
            <BODY>
            <IMG SRC="Query.asp" >
            </BODY>
            </HTML>[/code]

            Query.asp
            [code=asp]
            <%@ LANGUAGE="VBSCR IPT" %>
            <!-- #include file="../../includes/dbconn.asp"-->
            <%
            dim MyRs, sqlstr, MyConn

            Response.Expire s = 0
            Response.Buffer = TRUE
            Response.Clear
            Response.Conten tType = "image/gif"

            Set MyConn = Server.CreateOb ject("ADODB.Con nection")
            MyConn.Open dbConn
            Set MyRs = Server.CreateOb ject("ADODB.Rec ordset")

            sqlstr="select file from img"
            MyRs.Open sqlstr, MyConn, adOpenKeyset, adLockOptimisti c
            Response.Binary Write MyRs("file")
            Response.End

            MyRs.Close
            MyConn.Close

            set MyRs=nothing
            set MyConn= nothing
            %>[/code]

            My table is called img with fields id(int) and file (blob) . It's correct? why i still can't display.Thanks
            Last edited by JamieHowarth0; Dec 2 '07, 03:55 PM. Reason: Added code tags

            Comment

            • 848lu
              New Member
              • Dec 2006
              • 37

              #7
              hi, i was wandering....ca n you match the picture name with table primary key, so for my project i want to display a picture which also mathces with the primary key with a record, and display it on the screen, automatically.

              as when users click the menu from pictures menu, the code automatiicaly does this, and beside the picture i want to display the record details.


              thanks....

              Comment

              • 848lu
                New Member
                • Dec 2006
                • 37

                #8
                ok i guess nobody knows how to do it

                Comment

                Working...