Page 1 of 1

using variables in class[Solved]

Posted: Mon Sep 03, 2007 1:46 am
by shivam0101
Hello,

which is correct?

Code: Select all

Class Check
{
   var $mMemberId;

   function GetName($memberId)
  {
    $this->mMemberId=$memberId;

    $query=mysql_query("SELECT name FROM table WHERE mid=$mMemberId");// 1
  
                                         //   OR
    $query=mysql_query("SELECT name FROM table WHERE mid='. $this->mMemberId.'");  //2

}
}
1 or 2?


Thanks

Posted: Mon Sep 03, 2007 2:07 am
by s.dot
2

Although, since you're passing the memberid as a parameter to the function, you don't even need to use the class property.

Posted: Mon Sep 03, 2007 3:07 am
by josamoto

Code: Select all

$query = mysql_query("SELECT name FROM table WHERE mid = " .  $this->mMemberId);
I think you can omit $this, but for unambiguity, I always add $this.

:wink:

Posted: Mon Sep 03, 2007 3:19 am
by CoderGoblin
For a full understanding of why it is the second option you may want to refresh your memory by following this link to php.net - Strings

Posted: Mon Sep 03, 2007 11:04 am
by Z3RO21
Yup the 2nd option but I think you ment this:

Code: Select all

$query=mysql_query("SELECT name FROM table WHERE mid='{$this->mMemberId}'");
Notice you did not need the string concentration ( the periods ) and I also used {} brackets since the variable is in object context (would be the same in array contexts {$foo['bar']} )