PDA

View Full Version : random picture problem


0810
11-13-2002, 09:29 PM
hi how are you doing?

I try to run this script but not working. when a user visits my site, pic(picture) should change. However, it doesn't change at all.

It keeps same picture.

Could you please tell me what is wrong with my script.

my php is 4.23

here is code


<?php

$con=mysql_connect("localhost","usrname",password]) or die("couldn't connect'");
$db=mysql_select_db("database",$con) or die("couldn't select'");

$result = mysql_query ( "SELECT name, country, pic
FROM images
ORDER BY rand()
LIMIT 1" ) or die ("couldn't execute query" . mysql_error());



while ( $rst = mysql_fetch_array ( $result ) ) {
print("<img src=\"image/funny.gif\" width=100 height=100 style=\"margin-left:280;\"><img src=\"image/word.gif\">");
print("<table border=5 cellpadding=2 cellspacing=3 width=40% align=center>");
print("<tr>");
print("<td colspan=2>");
print("<center><img src=\"$rst[pic]\" width=400 height=400></center>");
print("</td>");
print("</tr>");
print("<tr>");
print("<td width=50>Link:</td>");
print("<td>$rst[link]</td>");
print("</tr>");
print("<tr>");
print("<td width=50>Comments:</td>");
print("<td width=350>$rst[comment]</td>");
print("</tr>");

}





?>
<html>
<head>
<meta http-equiv="Page-Enter" content="revealTrans(Duration=4,Transition=23)">
</head>
<body>



</body>

</html>

Nightfire
11-13-2002, 11:09 PM
Give this a try, I've not tested it so it might not work

$con=mysql_connect("localhost","usrname",password]) or die("couldn't connect'");
$db=mysql_select_db("database",$con) or die("couldn't select'");

$result = mysql_query ( "SELECT name, country, pic
FROM images" ) or die ("couldn't execute query" . mysql_error());



if ( $rst = mysql_fetch_array ( $result ) ) {

$pic = shuffle($rst[pic]);
print("<img src=\"image/funny.gif\" width=100 height=100 style=\"margin-left:280;\"><img src=\"image/word.gif\">");
print("<table border=5 cellpadding=2 cellspacing=3 width=40% align=center>");
print("<tr>");
print("<td colspan=2>");
print("<center><img src=\"$pic\" width=400 height=400></center>");
print("</td>");
print("</tr>");
print("<tr>");
print("<td width=50>Link:</td>");
print("<td>$rst[link]</td>");
print("</tr>");
print("<tr>");
print("<td width=50>Comments:</td>");
print("<td width=350>$rst[comment]</td>");
print("</tr>");

}

Spookster
11-13-2002, 11:30 PM
Just a note....in that last code pulling all the records and then randomly picking one would be less efficient especially if their were a few hundred records.

Personally I would not use a database at all for this. Just set up a directory for your images and number them like 0001.jpg, 0002.jpg and so on. Then just generate a random number using php and use that to form the file name and echo it to the page.

0810
11-14-2002, 02:20 AM
my sql is latest version which is 3.23 something

So order by rand() is not working in this version.

So do you have any idea?

Thanks