Achy Achy -5 years ago 152
SQL Question

That php code display first image just

It's just display the first image and stop.
i think the loop stop after display the first image.
How can i fix it?

header('Content-type: image/JPG');
$username = "root";
$password = "";
$host = "localhost";
$database = "game_world";
@mysql_connect($host, $username, $password) or die("Can not connect to database: ".mysql_error());
@mysql_select_db($database) or die("Can not select the database: ".mysql_error());
$sql = mysql_query("SELECT image FROM test_image");
while($row = mysql_fetch_array($sql))
echo $row['image'];

Answer Source

This is your clue:

header('Content-type: image/JPG');

Presumably this is requested from something like an <img> tag, though it could be requested from anything really.

This can, by HTTP design, return one image. That's all. After all, if it returned more than one, how would the browser know what to do with that response?

This server-side code should include an input for some kind of identifier for the image, for example:


Then you'd use that in the query:

SELECT image FROM test_image WHERE id=?

That way the browser/client/etc. can specify which image is being requested. Then the browser/client/etc. can make multiple separate requests (with different identifiers) for multiple images.

So in order to have, say, a list of <img> tags, the page code itself would only select the list of identifiers. It would then output those tags:

<img src="image.php?id=1" /> 
<img src="image.php?id=2" />
<img src="image.php?id=3" />
<img src="image.php?id=4" />
<!--- etc. --->

Each such tag would result in a separate request to this image handler code, which would use that identifier to return the image for that request.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download