Page 1 of 1

Randomizer with a condition

Posted: Sat Jan 25, 2003 4:29 pm
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.

Posted: Sat Jan 25, 2003 4:44 pm
by m3mn0n

Posted: Sat Jan 25, 2003 8:08 pm
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.

Posted: Sun Jan 26, 2003 12:42 pm
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...

Posted: Tue Jan 28, 2003 10:53 am
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.

Posted: Tue Jan 28, 2003 12:01 pm
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

Posted: Tue Jan 28, 2003 4:18 pm
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!