webdevRefinery Forum: Passing variables between methods - webdevRefinery Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Rate Topic: -----

User is offline lenny247 

  • Group: Members
  • Posts: 10
  • Joined: 08-May 12
  • LocationUK
  • Expertise:HTML,CSS,PHP,Javascript,SQL

Posted 22 June 2012 - 09:32 AM (#1)

Passing variables between methods


Passing variables between methods
Hi, im trying to pass a variable from one method to another. The code i have so far is:
<?php

class MIT_DB 
{
      private $cxn;
     
      function __construct() 
      {
        $this->cxn = new mysqli('localhost', 'root', 'password', 'db') or die('Couldn\'t connect to the database');
      }
    
      function query() 
      {
        $query = 'SELECT id, name, description FROM data_table';
            
            if($stmt = $this->cxn->prepare($query))
            {
                $stmt->execute();
                $stmt->bind_result($id, $name, $description);
                $stmt->fetch();
            }
            else 
            {
                echo '<p>There was no content in the database</p>';
            }

      }
    
      function loadAssoc() 
      {
            while($row = $stmt)
            {
                echo "<p>$name</p>";
                echo "<p>$description</p>";
            }
      }
}

?>

0


User is offline Renegade 

  • 418 I'm a teapot
  • Group: Members
  • Posts: 748
  • Joined: 08-March 10
  • Expertise:HTML,CSS,PHP,Javascript,Node.js,SQL,Graphics

Posted 22 June 2012 - 09:43 AM (#2)

You need to scratch up on scope, my friend. You can pass variables from one function to another by declaring them much like you did the
private $cxn
. This stores them in the
$this
object (which you seem to have used :huh: ).
http://adriancooney.ieGithubTwitterDribbbleForrst
We all die. The goal isn't to live forever. The goal is to create something that will.

Array(16).join({}-{}) + " Batman!";
0


User is offline Mazc 

  • Group: Members
  • Posts: 27
  • Joined: 18-June 12
  • Expertise:HTML,CSS,PHP,SQL

Posted 22 June 2012 - 10:39 AM (#3)

Pretty much what Renegade said. Alternatively you can pass the variable via an parameter, (argument) to the method. But you would need to return the result from the first method and then call the second method with the variable as argument.
0


User is offline lenny247 

  • Group: Members
  • Posts: 10
  • Joined: 08-May 12
  • LocationUK
  • Expertise:HTML,CSS,PHP,Javascript,SQL

Posted 22 June 2012 - 02:02 PM (#4)

Thanks for pointing me in the right direction. Just needed to read up on scope and method properties.
0


Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Enter your sign in name and password


Sign in options
  Or sign in with these services