Solved

PHP MYSQL '->' explanation

Posted on 2009-04-04
16
294 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
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
 
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 110

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 110

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 110

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 110

Expert Comment

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

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

749 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