Page 1 of 1

returning blob from mysql database using php

Posted: Sun Dec 07, 2008 9:00 pm
by fairyprincess18
I'm trying to figure out how to return a blob image that's stored in a table in my mysql database.

here is my code:

<?php
$DBConnect = mysqli_connect("localhost", "root", "newpwd", "portfolio")
Or die("<p>Connection failed dude!</p>");
$SQLString = "SELECT * FROM projects";
$QueryResult = mysqli_query($DBConnect, $SQLString);
echo "<table width='10%' border='1'>";
echo "<tr><th>Project</th><th>Description</th><th>Link</th></tr>";
$Row = mysqli_fetch_row($QueryResult);
do {
//display proj_id, proj_desc
echo "<tr><td>{$Row[0]}<td>{$Row[1]}</td>";
//display proj_img
$id = $_GET['id'];
$query = mysql_query("SELECT port_img FROM projects WHERE id='".$id."'");
$row = mysql_fetch_array($query);
$content = $row['image'];
header('Content-type: image/jpg');
echo "<td>$Content;</td></tr>";
$Row = mysqli_fetch_row($QueryResult);
} while ($Row);
mysqli_close($DBConnect);
echo "</table>";
?>

Can anyone help me figure this out?

Thanks.

Re: returning blob from mysql database using php

Posted: Sun Dec 07, 2008 9:46 pm
by requinix
You can't display HTML and an image at the same time. Dedicate a PHP page to creating the image and use an <img> in the HTML page to link to it.

Re: returning blob from mysql database using php

Posted: Mon Dec 08, 2008 5:31 pm
by fairyprincess18
Here's my call in the HTML file:

echo "<td><img src='fetchImage.php?imgID=1' /></td></tr>";

and here's fetchImage.php:

<?php
$DBConnect = mysqli_connect("localhost", "root", "newpwd", "portfolio")
Or die("<p>Connection failed dude!</p>");
$x = mysql_real_escape_string($_GET['id'], $DBConnect);
$result = mysql_query("SELECT port_img FROM projects WHERE imgID=$x", $DBConnect);
$blobby = mysql_fetch_row($result);
header("Content-type: image/jpeg");
echo $blobby;
exit;
?>

it still doesn't work. can you tell me why?

Re: returning blob from mysql database using php

Posted: Mon Dec 08, 2008 6:01 pm
by requinix
1. You can't mix-and-match MySQL and MySQLi functions.
2. mysql_fetch_row returns a row, not the contents of a single field. Read the manual page.

Re: returning blob from mysql database using php

Posted: Mon Dec 08, 2008 9:20 pm
by fairyprincess18
i tried this:

Code: Select all

 
    <?php
        $DBConnect = mysqli_connect("localhost", "root", "newpwd", "portfolio")
            Or die("<p>Connection failed dude!</p>");
        $result = mysqli_query("SELECT * FROM projects");
        $Row = mysqli_fetch_row($result);
        header("Content-type: image/jpeg");
        echo "{$Row[2]}";
        exit;
    ?>
 
it still doesn't work though

Re: returning blob from mysql database using php

Posted: Mon Dec 08, 2008 9:55 pm
by requinix
Read the manual pages. Here is the one for mysqli_query and ... oh wait, there aren't any functions called "mysqli_fetch_row".