![]() |
A random entry from MYSQL database
php.net doesn't help it's in none of my books, and I am having some issues, cause I need to do this.
Here's the project, I am doing work on a Realtor website where on the index page there is a featured listing section. The database is set up to where there are the fields, mls, streetnum, streetname, city, state, zip as well as some other fields, too many to mention I guess for now. Well, I could just place a button on the admin and on a daily basis mark one of the listings as featured, however, I thought it would be better if on loading the page a random entry would appear. here's the issue. The following came from php.net but does not work. :
$sql = "SELECT * FROM listings ORDER BY RAND() LIMIT 1";And this really doesn't echo the mls entry at all. Any thoughts? Thanks Mike |
Umm... I am not sure you can use RAND() in that context. RAND() is a function, not a description as to how to go about sorting your entires. Wouldn't it be more convenient to select a random number, and just do "LIMIT <random_number>, 1" just verify rand is not greater than your row count (simple task).
|
where does $row get set? it look like you need to fill $row with your data. So somthing like this:
[php] <?PHP $sql = "SELECT * FROM listings ORDER BY RAND() LIMIT 1"; $result = @mysql_query($sql, $connection) or die(mysql_error()); $row = mysql_fetch_object($result); ?> </head> <body> <?PHP echo $row->mls; ?> [/php] try that. i haven't worked that much with MySQL, but it should work for you. I could be wrong, though, since you might have set $row above the code that you posted. |
It took some doing but I figured it out. I am posting it so that anyone who has this question can come and see...
:
$connection = mysql_connect("host","user","pass") or die(mysql_error());this is a realestate thing, so anything that is called mls anything is specific to my database. Hope this helps someone Mike |
Hi just to make you people know... I had done the same task using the original sql query posted. It is supposed to work on mysql servers latter than MySQL 3.23. Maybe the problem was in how you mannaged the connection or the variable resutl. I dont think you submited the whole code... again this is just for info.
happy to read you solve the problem! :D codetaino |
| All times are GMT -5. The time now is 3:50 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC