Randomizer with a condition

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Randomizer with a condition

Post by evilmonkey »

Hello.

I want to make a randomaizer that selects a row from a mysql database that meets a certain condition. I have no idea how to make randomizers (never worked with them before), so I will not be a slave driver and ask you people to write a code for me. But, please reccommend good tutorials, or perhaps functions that I could look up in the PHP manual. I want to work with php as much as possible and as little as possible with MySQL. So, who can be brave and give an answer? :D

Thanks guys.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

I have decided to let MySQL do my dirty work for me. The problem is MySQL doesn't do the work very well. :D

This is my code:

Code: Select all

$db = mysql_connect("localhost", "", "") or die ("could not connect to db");
mysql_select_db("dbthawowi_2") or die ("could not select database");
$query="SELECT joke FROM table WHERE id=2.50 AND validation='yes' ORDER BY RAND() LIMIT 1";
$result=mysql_query($sql, $db);
echo $result;
This displays nothing, and I want it to display the field 'joke'.

What is wrong?

Thanks.
Bitmaster
Forum Newbie
Posts: 20
Joined: Thu Nov 21, 2002 8:42 am

Post by Bitmaster »

Try this:


Code: Select all

<?php

$db = mysql_connect("localhost", "", "") or die ("could not connect to db"); 
mysql_select_db("dbthawowi_2") or die ("could not select database"); 
$sql="SELECT joke FROM table WHERE id=2.50 AND validation='yes' ORDER BY RAND() LIMIT 1"; 
$result=mysql_query($sql, $db); 

     $row = mysql_fetch_assoc($result);
     echo $row["joke"];


?>
Also I'm not sure that the SQL query you indicated would work...
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

The above code worked, thanks.

I have another question. I want it to select a random row every 24 hours. Is there a way to do that?

Thanks.
DeGauss
Forum Contributor
Posts: 105
Joined: Tue Oct 22, 2002 9:44 am
Location: Gainesville, FL

Post by DeGauss »

If you're using a windows machine, add a scheduled task (Start > Programs > Accessories > System Tools > Scheduled Tasks)

if you're using *nix use crontab -e
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

I want to do this from PHP with a code, not with a nix macine. I don't run my own server :(

Cheers!
Post Reply