Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    mySQL image loading help

    Hi everyone,

    I'm having trouble loading the image that i have stored in mySQL.
    I'm getting a header error and loads of symbols:

    Code:
    Warning: Cannot modify header information - headers already sent by (output started at /home/sites/onestopremovalsshop.co.uk/public_html/shop.php:9) in /home/sites/onestopremovalsshop.co.uk/public_html/shop.php on line 174
    This is the script that i have used to send the image to mySQL if it helps:

    Code:
    <?
    $dbhost = "localhost";
    $dbname = "********";
    $dbuser = "********";
    $dbpass = "********";
    
    $connect = mysql_connect ($dbhost, $dbuser, $dbpass) or die("CAN NOT CONNECT:".mysql_error()); 
    mysql_select_db ($dbname);
    
    $name = $_POST['name'];
    $des = $_POST['des'];
    $no = $_POST['no'];
    $price = $_POST['price'];
    
    if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) { 
    
          // Temporary file name stored on the server
          $tmpName  = $_FILES['image']['tmp_name'];  
           
          // Read the file 
          $fp      = fopen($tmpName, 'r');
          $data = fread($fp, filesize($tmpName));
          $data = addslashes($data);
          fclose($fp);
          
    
          // Create the query and insert
          // into our database.
          $query = "INSERT INTO image(image, name, des, no, price) VALUES ('$data','$name', '$des', '$no', '$price')";
          $results = mysql_query($query, $connect);
          
          // Print results
          print "Thank you, your image and data has been uploaded.";
          
    }
    else {
       print "No image selected/uploaded";
    }
    
    mysql_close($connect);
    ?>
    This is the code that i am using to view the image and data. I have know problem viewing the data just image.

    Code:
    <?php
    $host="localhost"; 
    $username="web180-shop-1";
    $password="ryan2008";
    $db_name="web180-shop-1";
    $tbl_name="image";
    
    mysql_connect("$host", "$username", "$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");
    
    $sql="SELECT * FROM $tbl_name ORDER BY id DESC";
    $result=mysql_query($sql);
    ?>
    <table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
    <tr>
    <td width="4%" align="center" bgcolor="#0000FF"><strong class="style4">#</strong></td>
    <td width="12%" align="center" bgcolor="#0000FF"><strong class="style4">Name</strong></td>
    <td width="8%" align="center" bgcolor="#0000FF"><strong class="style4">Price</strong></td>
    <td width="7%" align="center" bgcolor="#0000FF"><strong class="style4">No of</strong></td>
    <td width="69%" align="center" bgcolor="#0000FF"><span class="style4"><strong>Discription</strong></span></td>
    </tr>
    
    <?php
    while($rows=mysql_fetch_array($result)){ // Start looping table row
    ?>
    <tr>
    <td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
    <td bgcolor="#FFFFFF"><? echo $rows['name']; ?><BR></td>
    <td align="center" bgcolor="#FFFFFF"> <? echo $rows['price']; ?></td>
    <td align="center" bgcolor="#FFFFFF"><? echo $rows['no']; ?></td>
    <td align="center" bgcolor="#FFFFFF"><? echo $rows['des']; ?></td>
    <? header('Content-type: image/jpg'); ?>
    <td align="center" bgcolor="#FFFFFF"><? echo $rows['image]; ?></td>
    </tr>
    
    <?php
    // Exit looping and close connection
    }
    mysql_close();
    ?>
    </table>
    Thanks for the help that you may be able to give me.

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    Quote Originally Posted by tecmeister View Post
    Code:
    <?php
    $host="localhost"; 
    $username="web180-shop-1";
    $password="ryan2008";
    $db_name="web180-shop-1";
    $tbl_name="image";
    
    mysql_connect("$host", "$username", "$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");
    
    $sql="SELECT * FROM $tbl_name ORDER BY id DESC";
    $result=mysql_query($sql);
    ?>
    <table width="90&#37;" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
    <tr>
    <td width="4%" align="center" bgcolor="#0000FF"><strong class="style4">#</strong></td>
    <td width="12%" align="center" bgcolor="#0000FF"><strong class="style4">Name</strong></td>
    <td width="8%" align="center" bgcolor="#0000FF"><strong class="style4">Price</strong></td>
    <td width="7%" align="center" bgcolor="#0000FF"><strong class="style4">No of</strong></td>
    <td width="69%" align="center" bgcolor="#0000FF"><span class="style4"><strong>Discription</strong></span></td>
    </tr>
    
    <?php
    while($rows=mysql_fetch_array($result)){ // Start looping table row
    ?>
    <tr>
    <td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
    <td bgcolor="#FFFFFF"><? echo $rows['name']; ?><BR></td>
    <td align="center" bgcolor="#FFFFFF">&#163; <? echo $rows['price']; ?></td>
    <td align="center" bgcolor="#FFFFFF"><? echo $rows['no']; ?></td>
    <td align="center" bgcolor="#FFFFFF"><? echo $rows['des']; ?></td>
    <? header('Content-type: image/jpg'); ?>
    <td align="center" bgcolor="#FFFFFF"><? echo $rows['image]; ?></td>
    </tr>
    
    <?php
    // Exit looping and close connection
    }
    mysql_close();
    ?>
    </table>
    you can only send one content-type per request...so, you script is already sending text/html for the html content and hence you cannot change it in the middle

    what you can do is have a separate php script that only serves up the image...sorta like this

    img.php
    PHP Code:
    <? 
    $host
    ="..."
    $username="...";
    $password="...";
    $db_name="...";
    $tbl_name="image";

    mysql_connect("$host""$username""$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");

    $id $_GET["id"];
    // sanitize $id !!
    $sql="SELECT image FROM $tbl_name WHERE id=" $id;
    $result=mysql_query($sql);
    header('Content-type: image/jpg');
    if ( 
    $rows=mysql_fetch_array($result) ) {
      echo 
    $rows[image]; 
    } else {
      
    // serve up a default
    }
    ?>
    and in your script change it

    PHP Code:
    <tr>
    <td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
    <td bgcolor="#FFFFFF"><? echo $rows['name']; ?><BR></td>
    <td align="center" bgcolor="#FFFFFF">£ <? echo $rows['price']; ?></td>
    <td align="center" bgcolor="#FFFFFF"><? echo $rows['no']; ?></td>
    <td align="center" bgcolor="#FFFFFF"><? echo $rows['des']; ?></td>
    <td align="center" bgcolor="#FFFFFF"><img src="img.php?id=<? echo $rows['id']; ?>"></td>
    </tr>
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • #3
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have already tried that and i seem to still get the same header error.
    I have looked up the error and mosr people say tat it is white space.

    I have check the whole thing through, even created a new php file and still get the same error.

    Is there anyother way of loading the image from mySQL?

    Thanks for your help tho much appricated.

    tecmeister


  •  

    Posting Permissions

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