Solved

PHP MYSQL '->' explanation

Posted on 2009-04-04
16
286 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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.
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

708 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

18 Experts available now in Live!

Get 1:1 Help Now