Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.