Jump to content

trouble with pulling the right image


elrobbo6

Recommended Posts

So I am trying to pull the first image that was uploaded to the database.  But for some reason this only works part of the time and if you delete any image in the database it seems to just pic another another random one.  Any suggestions?

 

<?php

{

$row = getad();

header("Content-type: image/jpeg");

$pic = ($row['image']);

print "$pic";

}

 

function getad()

{

$ads = array();

mysql_connect("localhost","******","********");

mysql_select_db("users");

 

 

$query  = "SELECT image FROM image_ads";

$result = mysql_query($query);

 

$i = 0;

 

while($row = mysql_fetch_array($result))

{

  $i ++;

  $ads[$i] = $row;

}

 

$id = date("z");

$index = $id % $i + 1;

 

mysql_close();

return $ads[$index];

}

 

?>

 

Link to comment
https://forums.phpfreaks.com/topic/236679-trouble-with-pulling-the-right-image/
Share on other sites

Do you have a field that auto increments in the database?  If you do, you can put the name of that field where I have "id" below, and it should work.  If you want to pull the last image that was uploaded, replace "ASC" with "DESC".

 

$query  = "SELECT image FROM image_ads ORDER BY id ASC";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.