Re: Display just 1 record in a query

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Op 1/13/10 12:43 AM, Robert Cummings schreef:
> 
> dealtek@xxxxxxxxx wrote:
>> On Jan 12, 2010, at 1:57 PM, Ashley Sheridan wrote:
>>
>>> 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
> 
> Put your random logic into the query:
> 
>     SELECT
>         something
>     FROM
>         somewhere
>     WHERE
>         condition
>     ORDER BY
>         RAND()
>     LIMIT
>         1

please read the following to understand the consequences and
possible performance problems with this approach:

http://www.titov.net/2005/09/21/do-not-use-order-by-rand-or-how-to-get-random-rows-from-table/

personally I go with the 2 query approach

 ... or even to use a specific field to store a generated random value
(with suitable index) for each record, the stored values can be regenerated
in an out-of-band process (e.g. cron job) ... such a setup would suffice in
situations where frontend response speed is of primary concern and it's
acceptable to return the same 'random' record over a given period (i.e.
the time between random value regeneration)

> Cheers,
> Rob.


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux