Solved

PHP MYSQL '->' explanation

Posted on 2009-04-04
16
288 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:allanch08
  • 6
  • 4
  • 3
  • +2
16 Comments
 
LVL 28

Assisted Solution

by:gamebits
gamebits earned 130 total points
ID: 24066957
Programing using object

 $offset_result = mysql_query( " SELECT FLOOR(RAND() * COUNT(*)) AS `offset` FROM `table` "); //query
$offset_row = mysql_fetch_object( $offset_result );  //fetching object of the query result
$offset = $offset_row->offset;  //assign the value of the resultset alias offset as you can see in the query to the variable
                                                    $offset
$result = mysql_query( " SELECT * FROM `table` LIMIT $offset, 1 " ); // running another query using the value of $offset
                                                                                                                   obtained from the first query
 
0
 
LVL 28

Expert Comment

by:gamebits
ID: 24066962
0
 
LVL 3

Accepted Solution

by:
techmaza earned 160 total points
ID: 24066970
By firing 1st sql query u r taking integral part of answer in a field called offset
                   
              FLOOR(RAND() * COUNT(*)) AS `offset`
now in a second line

     $offset_row = mysql_fetch_object( $offset_result );

u r fetching a row from recordset offset_result

now in third line

  $offset = $offset_row->offset;

u r saving value of the offset field in ur offset variable
Remember result of sql query is generating a table with field name offset and u r using a variable offset to get the value in ur offset field

if u change ur first line in this way

$offset_result = mysql_query( " SELECT FLOOR(RAND() * COUNT(*)) AS `offset_field_name` FROM `table` ");

then ur third line wud be

$offset = $offset_row->offset_field_name;

I think this shuld clear ur doubt.




0
 
LVL 14

Expert Comment

by:shobinsun
ID: 24066975
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'.

0
 

Author Comment

by:allanch08
ID: 24066978
thanks. i echoed $offset_row and got the value 'object'.

so is there an official term for the syntax '->'?
0
 

Author Comment

by:allanch08
ID: 24066984
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
0
 
LVL 14

Assisted Solution

by:shobinsun
shobinsun earned 60 total points
ID: 24066988
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'.

0
 

Author Comment

by:allanch08
ID: 24067081
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?
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 14

Assisted Solution

by:shobinsun
shobinsun earned 60 total points
ID: 24067150
Hello,

$offset_row is an Object of class stdClass.

So you have to use the variable with this object for printing the result.

Regards.

0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 150 total points
ID: 24068485
The PHP man pages that describe Object Oriented Programming are located here:
http://us.php.net/manual/en/language.oop5.php

Here is the simplest OOP example I have in my inventory.  It teaches some of the basics.

HTH, ~Ray
<?php // RAY_oop_vars.php

error_reporting(E_ALL);
 

// DECLARE THE CLASS

Class MyOOP

{

   public  $my_public_var  = 'open';

   private $my_privat_var;
 

// DECLARE THE CONSTRUCTOR

   public function __construct()

   {

      $this->my_privat_var = 'closed';

   }
 

// DECLARE A FUNCTION

   public function show_vars()

   {

      echo "<br/>\n";

      echo "<br/>PUBLIC $this->my_public_var \n";

      echo "<br/>PRIVAT $this->my_privat_var \n";

   }
 

// DECLARE ANOTHER FUNCTION

   public function set_privat($value)

   {

      $this->my_privat_var = $value;

   }

// END CLASS DECLARATION

}
 
 
 
 

// INSTANTIATE THE CLASS AND SHOW THE INITIAL VALUES OF VARIABLES

$a = new MyOOP;

$a->show_vars();
 

// SET A PRIVATE VARIABLE VIA THE FUNCTION

$a->set_privat('NewValue');
 

// SET A PUBLIC VARIABLE DIRECTLY

$a->my_public_var = 'Cheese';
 

// SEE THE NEW VALUES

$a->show_vars();
 

// DUMP THE VALUES FROM THE OBJECT

echo "<pre>";

var_dump($a);
 

// TRY TO SET A PRIVATE VARIABLE THE WRONG WAY

$a->my_privat_var = 'Beer';

Open in new window

0
 

Author Comment

by:allanch08
ID: 24070764
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 " );

0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 150 total points
ID: 24071807
One note: floor() rounds DOWN to the next integer value.  IIRC, ceil() rounds up and round() rounds arithmetically.

To the question about $offset=3, the answer is "yes" - see the slight differences in the code snippet.

HTH, ~Ray
// ORIGINAL CODE SNIPPET

$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 " );
 

// NON-OOP CODE SNIPPET

$offset_result = mysql_query( " SELECT FLOOR(RAND() * COUNT(*)) AS `offset` FROM `table` ");

$offset_row = mysql_fetch_assoc( $offset_result );

$offset = $offset_row["offset"];

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

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 24071834
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.
0
 

Author Comment

by:allanch08
ID: 24072094
cool, thanks for the help ray
0
 

Author Closing Comment

by:allanch08
ID: 31566554
thanks for help everyone
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 24072382
Thanks for the points - it's a good question, ~Ray
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now