[CS-FSLUG] php sql question

David McGlone david at dmcentral.net
Wed Dec 16 20:17:41 CST 2009

Hey all,

I've been working on a simple PHP MySQL script that will change a picture on a 
certain day every month as to try and understand PHP and MySQL better. In this 
particular code I'm trying to learn about the rand() function and I'm 
wondering if it can be controlled. For instance I'm trying to use the random 
function to grab a random row from a database and display that row for a set 
amount of time. I have some code (pasted below) that I have been experimenting 
with, but for the life of me, I can't think of a way to get the row to stick 
when the browser is refreshed.

My train of thought on how to accomplish this is there (at least I think it 
is) LOL, but I just can't get it out into my code. I created steps to try and 
sort out my thought process step by step like this:

1. Grab a random row from the DB on a given day lets say the 16th.
2. display that row from this day (16th) up until the 16th of next month.

simple enough, but I'm stumped. When I use my code, with the rand() function 
in the SQL query, the row always changes on a refresh of the browser. If I try 
and use the exit; function then the rest of the page is cut short and none of 
the php code is executed below the exit; function.

Here's the code I've been trying to work it out with so far:

$today = date('j');

if ($today == 16) {
$query = "SELECT * FROM monthlyPooch ORDER BY RAND() LIMIT 1";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
$name = "<p>$row[1]:  $row[4] - $row[5] - $row[6]</p>";

echo "$name";
echo "<img src=images/$row[3]";

There's got to be a way I can stop the rand() function from executing more 
than once.

Would anyone by any chance have an idea?

David M.
I have been driven to my knees many times by the overwhelming conviction that 
I had nowhere else to go.

More information about the Christiansource mailing list