Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 302
  • Last Modified:

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

0
allanch08
Asked:
allanch08
  • 6
  • 4
  • 3
  • +2
6 Solutions
 
gamebitsCommented:
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
 
gamebitsCommented:
0
 
techmazaCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
shobinsunCommented:
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
 
allanch08Author Commented:
thanks. i echoed $offset_row and got the value 'object'.

so is there an official term for the syntax '->'?
0
 
allanch08Author Commented:
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
 
shobinsunCommented:
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
 
allanch08Author Commented:
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
 
shobinsunCommented:
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
 
Ray PaseurCommented:
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
 
allanch08Author Commented:
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
 
Ray PaseurCommented:
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
 
Ray PaseurCommented:
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
 
allanch08Author Commented:
cool, thanks for the help ray
0
 
allanch08Author Commented:
thanks for help everyone
0
 
Ray PaseurCommented:
Thanks for the points - it's a good question, ~Ray
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now