using variables in class[Solved]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shivam0101
Forum Contributor
Posts: 197
Joined: Sat Jun 09, 2007 12:09 am

using variables in class[Solved]

Post 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
Last edited by shivam0101 on Mon Sep 03, 2007 5:05 am, edited 1 time in total.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
josamoto
Forum Commoner
Posts: 41
Joined: Fri Aug 24, 2007 6:57 am
Location: South Africa
Contact:

Post 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:
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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
Z3RO21
Forum Contributor
Posts: 130
Joined: Thu Aug 17, 2006 8:59 am

Post 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']} )
Post Reply