Jump to content

[SOLVED] pop array from mysql


guyfromfl

Recommended Posts

I don't understand why this doesn't work

 

for background...I want to make a script to randomly choose a flyer picture for upcomming shows for a band.

php knows theres something comming back from mysql because $display always has a value, just no picture..

 

$sql = "SELECT loc FROM flyers WHERE showDate >= curdate()";
$result = mysql_query($sql);

$flyer = array();
$ct = 1;

while ($row = mysql_fetch_array($result))
{
$flyer[$ct] = $row[1];
$ct++;
}

$display = rand(1,(sizeof($flyer)));

print "<img src=$flyer[$display]>" . $display . $flyer[$display];

 

you can see the output of that here http://alchemy.servebeer.com/test/ad.php

Link to comment
https://forums.phpfreaks.com/topic/85568-solved-pop-array-from-mysql/
Share on other sites

You can use MySQL to select a random record, which makes this much easier:

 

$sql = "SELECT loc FROM flyers WHERE showDate >= curdate() ORDER BY rand() LIMIT 1";
$result = mysql_query($sql);

$randomRecord = mysql_fetch_array($result));

print "<img src=$randomRecord[0]>" . $randomRecord[0];

 

EDIT: Your problem is that you are only selecting 1 item from MySQL and you are creating your array based on "$row[1];". There is no value for that, the first value would be at index 0.

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.