Re: [PHP] How do I access a local variable?

2009-04-19 Thread Chris

abdulazeez alugo wrote:

Hi guys,

I have a function inside which I set alocal variable to store a result. Can I 
access this variable in another function? if yes then how?


No, you can't. You either need to pass it back (recommended) or make it 
global (not recommended).



function tbl1($entrytitle, $entrytext)

{

global $conn;

$result= mysql_query(INSERT INTO tbl1(tbl1_id, title, text)

 VALUES('NULL', '$entrytitle', '$entrytext'), $conn);

 $tbl_id=mysql_insert_id($conn);// this is the local variable I'm setting 
to get the last auto increment id


return $tbl_id;

} 

 


Now I wish to access that variable in another function thus:

function tbl2($name, $entrytitle, $entrytext)

{


  $other_id = tbl1($entrytitle, $entrytext);
  echo $other_id;


global $conn;

$result =mysql_query(INSERT INTO tbl2(tbl1_id, name, title, text)

   VALUES('$tbl1_id', '$name', '$entrytitle', '$entrytext' ), $Conn); 


}


PS : look up mysql_real_escape_string for when you save your data.

--
Postgresql  php tutorials
http://www.designmagick.com/


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] How do I access a local variable?

2009-04-19 Thread Paul M Foster
On Mon, Apr 20, 2009 at 12:54:27AM +0100, abdulazeez alugo wrote:

 
 Hi guys,
 
 I have a function inside which I set alocal variable to store a result. Can I 
 access this variable in another function? if yes then how?
 
 function tbl1($entrytitle, $entrytext)
 
 {
 
 global $conn;
 
 $result= mysql_query(INSERT INTO tbl1(tbl1_id, title, text)
 
  VALUES('NULL', '$entrytitle', '$entrytext'), $conn);
 
  $tbl_id=mysql_insert_id($conn);// this is the local variable I'm setting 
 to get the last auto increment id
 
 } 
 
  
 
 Now I wish to access that variable in another function thus:
 
 function tbl2($name, $entrytitle, $entrytext)
 
 {
 
 global $conn;
 
 $result =mysql_query(INSERT INTO tbl2(tbl1_id, name, title, text)
 
VALUES('$tbl1_id', '$name', '$entrytitle', 
 '$entrytext' ), $Conn); 
 
 }
 
  
 
 Or is there a better way to store the variable?

If a variable is local to a function, there is no way to access that
variable outside that function. You can pass it back as a return value
from the original function, or make it global (not advised) to make it
visible in other functions.

Paul

-- 
Paul M. Foster

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] How do I access a local variable?

2009-04-19 Thread Ashley Sheridan
On Sun, 2009-04-19 at 21:55 -0400, Paul M Foster wrote:
 On Mon, Apr 20, 2009 at 12:54:27AM +0100, abdulazeez alugo wrote:
 
  
  Hi guys,
  
  I have a function inside which I set alocal variable to store a result. Can 
  I access this variable in another function? if yes then how?
  
  function tbl1($entrytitle, $entrytext)
  
  {
  
  global $conn;
  
  $result= mysql_query(INSERT INTO tbl1(tbl1_id, title, text)
  
   VALUES('NULL', '$entrytitle', '$entrytext'), $conn);
  
   $tbl_id=mysql_insert_id($conn);// this is the local variable I'm 
  setting to get the last auto increment id
  
  } 
  
   
  
  Now I wish to access that variable in another function thus:
  
  function tbl2($name, $entrytitle, $entrytext)
  
  {
  
  global $conn;
  
  $result =mysql_query(INSERT INTO tbl2(tbl1_id, name, title, text)
  
 VALUES('$tbl1_id', '$name', '$entrytitle', 
  '$entrytext' ), $Conn); 
  
  }
  
   
  
  Or is there a better way to store the variable?
 
 If a variable is local to a function, there is no way to access that
 variable outside that function. You can pass it back as a return value
 from the original function, or make it global (not advised) to make it
 visible in other functions.
 
 Paul
 
 -- 
 Paul M. Foster
 

Or pass it by reference to the functions, so that both are able to make
changes to it.



Ash
www.ashleysheridan.co.uk


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php