elrobbo6 Posted May 17, 2011 Share Posted May 17, 2011 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 More sharing options...
zer0day Posted May 17, 2011 Share Posted May 17, 2011 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"; Link to comment https://forums.phpfreaks.com/topic/236679-trouble-with-pulling-the-right-image/#findComment-1216681 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.