Jump to content

Recommended Posts

Posted

Im sure Im missing something obvious here, but its still not working correct. Im trying to display 3 random images from a database... I don't get any coding errors, parse errors, but it displays nothing?

 

Here's the code:

 

<?php

 

$result = mysql_query("SELECT media_id, thumbnail, file, path FROM media_info WHERE media_id = RAND() LIMIT 3")

or die(mysql_error());

 

while($row=mysql_fetch_array($result))

{

?>

<a href="<?=$row[path];?>?media_id=<?=$row[media_id];?>"><img src="<?=$row['thumbnail']?>" alt="" width="100" height="80"></a></td>

<br>

<?

}

?>

 

Thanks for any help

Posted

how about frst listing everything in $row then reformat the output in a print statement as indicated below:

><?php
$result = mysql_query("SELECT media_id, thumbnail, file, path FROM media_info WHERE media_id = RAND() LIMIT 3") or die(mysql_error());

//  test what is in $row 
//
print "<table=border=1>\n";
while($row=mysql_fetch_array($result)) {
 print "<tr>\n";
 foreach ($row as $field=>$val )
 print "<td>$field</td><td>$val</td>";
 print "</tr>\n";
}
Print "</table><br>\n";

// reformat results in a print statement
//
while($row=mysql_fetch_array($result)) {
print "<a href=\".$row['path']."?media_id=".$row['media_id']."><img src=\".$row['thumbnail'].">alt=\"\" width=\"100\" height=\"80\"></a><br>
}
?>
Posted

Hi Airjunkie

 

I haven't used a lot of the mysql functions, but have had a look at the mysql manual, which says RAND() returns a random floating-point value in the range from 0 to 1.0. You need to make sure that there is a row in the database for media_id, which appears unlikely for the statment "media_id = RAND()".

 

maybe replace the where clause with "media_id = (SELECT media_id FROM media_info order by RAND() limit 3 )" might work?

 

Peter

 

 

 

 

Im sure Im missing something obvious here, but its still not working correct. Im trying to display 3 random images from a database... I don't get any coding errors, parse errors, but it displays nothing?

 

Here's the code:

 

<?php

 

$result = mysql_query("SELECT media_id, thumbnail, file, path FROM media_info WHERE media_id = RAND() LIMIT 3")

or die(mysql_error());

 

while($row=mysql_fetch_array($result))

{

?>

<a href="<?=$row[path];?>?media_id=<?=$row[media_id];?>"><img src="<?=$row['thumbnail']?>" alt="" width="100" height="80"></a></td>

<br>

<?

}

?>

 

Thanks for any help

Posted

Turns out you were right about the rand statement, it was retuning a media_id that did not exist in the database, so instead I just user ORDER BY RAND() and then displayed 3 results, thanks both of you.

Posted

airjnkie2000,

 

not sure if you tried out the modified script i provided you but note that you would know that your $result is empty by printing out its contents to verify it. It is a good idea to add this block of code whenever you are troubleshooting calls to mysql.

>//  test what is in $row
//
print "<table=border=1>\n";
while($row=mysql_fetch_array($result)) {
 print "<tr>\n";
 foreach ($row as $field=>$val )
 print "<td>$field</td><td>$val</td>";
 print "</tr>\n";
}
Print "</table><br>\n";

Join the conversation

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

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

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