Page 1 of 1

[Solved]How to hide data value?

Posted: Fri Oct 20, 2006 1:21 am
by kpraman

Code: Select all

<?php



      public function testing_insertData()
          {
	        $new=new MemberTypes;
                $new->AddOrUpdateMemberType('manager','Head of    the','true','','membertype');
                echo $memid=mysql_insert_id();
	$resExpected=array((string)$memid,'manager','Head of the','true');
		   
	$query=mysql_query("SELECT * FROM membertype WHERE membertypeid=$memid");
                $resActual=mysql_fetch_row($query);
	$this->assertEquals($resExpected,$resActual, "COULD NOT INSERT DATA.");
          }
	
	
		 
       public function testing_insertNotable()
           {
                 $new=new MemberTypes;
                 $new->AddOrUpdateMemberType('manager','Head of the','true','','');
                 echo $memid=mysql_insert_id();
                 $query=mysql_query("SELECT * FROM membertype WHERE membertypeid=$memid");
                 $resActual=mysql_fetch_row($query);
	 $this->assertEquals(0,sizeof($this->resActual), "COULD NOT INSERT DATA.");
           
            }



?>
The value of the first method's $memid gets assigned to second method's $memid. I do not want to change $memid name. How to restrict this?

Posted: Fri Oct 20, 2006 1:08 pm
by feyd
I have no idea what you're asking. Please explain.

Posted: Tue Oct 31, 2006 12:53 am
by kpraman
what i meant is,


methods should be public, the variables inside it should private. I should be able to give same variable name and they should not be overwritten by their previous values.

Posted: Tue Oct 31, 2006 3:01 am
by CoderGoblin
If I understand your question the Static varable declaration may be of use...

Code: Select all

function test($dummy=null)
{
  static $test='';

  if ($dummy !== null) {
      $test=$dummy;
  }

  return $test;
}

echo test('Hello'); // returns 'Hello'
echo test(); // returns 'Hello'

Posted: Tue Oct 31, 2006 6:05 am
by Maugrim_The_Reaper
My guess is that you're looking at it the wrong way. $memid is being assigned the value of mysqli_insert_id(). Assuming you have not created a new record, mysqli_insert_id() will continue providing the primary key of the last insert over, and over, and over again until you make a new insert.

Your AddOrUpdateMemberType function might be coopted here - have it return mysqli_insert_id() or false depending on whether something is added, or just updated (no new id). Otherwise the Id test is a bit defunct since its not strictly a test of the class and cannot tell which operation (Insert/Update) has taken place.

From there you can test both scenarios, with a false id indicating an update. In an update scenario, verify by selecting based non-id data. Even better, since the id should be a primary key, make $memid a private class property of your test class. Set it on the initial insert, then re-use it for subsequent updates/verification selects...