Link to home
Start Free TrialLog in
Avatar of allanch08
allanch08Flag for United Kingdom of Great Britain and Northern Ireland

asked on

PHP MYSQL '->' explanation

Hello experts,

This should be an easy one. I'm trying to understand the code below but I'm stuck on line 3:

$offset = $offset_row->offset;

can someone tell me what happens in this line and what the '->' does. thanks
$offset_result = mysql_query( " SELECT FLOOR(RAND() * COUNT(*)) AS `offset` FROM `table` ");
$offset_row = mysql_fetch_object( $offset_result );
$offset = $offset_row->offset;
$result = mysql_query( " SELECT * FROM `table` LIMIT $offset, 1 " );

Open in new window

SOLUTION
Avatar of gamebits
gamebits
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hello,

You have select the result   " FLOOR(RAND() * COUNT(*))  "  as   ' offset '.

So you have to fetch the value from the resulted row by using this pointer 'offset'.

Avatar of allanch08

ASKER

thanks. i echoed $offset_row and got the value 'object'.

so is there an official term for the syntax '->'?
thanks for the help everyone. that's cleared it up. This is my first foray into OOP which is why the structure of it looked a bit strange
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'm following example 3 here - http://akinas.com/pages/en/blog/mysql_random_row/ . I'm trying to visualize it using an example as that's the best way for me to get a grasp of it. This is what I have so far:

If i have a table with 5 records - 1, 2, 3, 4 ,5

COUNT (*) would = 5.
RAND() would select a number of between 0 and 1. Say - 0.5.

RAND() * COUNT(*) = 0.5 * 5 = 2.5

FLOOR would make float into an integer = 3.

This value would be set AS offset. so 'offset' =3

Then I run the query and make $offset_row = 3

So in line 3 where it states '$offset = $offset_row->offset;' how is this visualised?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks. just one final question. could the author of the script have done the same without OOP and just straight forward PHP. ie. he could just have got the number as an integer and placed it in the final sql query. So if the $offset = 3

and final query would be

$result = mysql_query( " SELECT * FROM `table` LIMIT 3, 1 " );

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oh, one other thing.  mysql_query() gives a return value - either a "resource-id" or FALSE.  You always want to test that value to see if the query worked.  If it returns FALSE, print out the value of mysql_error() to see what went awry.
cool, thanks for the help ray
thanks for help everyone
Thanks for the points - it's a good question, ~Ray