allanch08
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
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 " );
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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'.
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'.
ASKER
thanks. i echoed $offset_row and got the value 'object'.
so is there an official term for the syntax '->'?
so is there an official term for the syntax '->'?
ASKER
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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 " );
and final query would be
$result = mysql_query( " SELECT * FROM `table` LIMIT 3, 1 " );
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
ASKER
cool, thanks for the help ray
ASKER
thanks for help everyone
Thanks for the points - it's a good question, ~Ray
http://www.devarticles.com/c/a/PHP/Object-Oriented-Programming-in-PHP/