?
Solved

PHP MYSQL '->' explanation

Posted on 2009-04-04
16
Medium Priority
?
298 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 520 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 640 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 240 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 240 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 600 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 600 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 111

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 111

Expert Comment

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
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 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 …
Suggested Courses

770 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