Re: Display just 1 record in a query
| From: | Robert Cummings | Date: | Tue, 12 Jan 2010 23:43:34 +0000 |
| Subject: | Re: Display just 1 record in a query | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-301050@lists.php.net to get a copy of this message | ||
dealtek@gmail.com wrote:
On Jan 12, 2010, at 1:57 PM, Ashley Sheridan wrote:Put your random logic into the query:Depends on how you're creating running the query. You could do something like: echo mysql_result($result, 1, 'fieldname'); Where $result is your result object and 1 is a 0 indexed array, so would be the second result.Thanks Ryan, Ashley & Kim for the good techniques... - in my case I was trying to pull a random record from a query of Table1 - then do a 2nd query from 1st so mysql_result worked fine for my needs like: ... do query 1... 'cur' - SELECT id FROM myTable1 $ran = rand(0, $totalRows_cur - 1); // pick random rec row within total count $pick1 = mysql_result($cur, $ran); // get the ID from the choice - (like id=252) ... do query 2 // where relatedID = $pick1 of myTable2
SELECT
something
FROM
somewhere
WHERE
condition
ORDER BY
RAND()
LIMIT
1
Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP