Re: How do I access a local variable?
| From: | Chris | Date: | Mon, 20 Apr 2009 00:30:11 +0000 |
| Subject: | Re: How do I access a local variable? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-291668@lists.php.net to get a copy of this message | ||
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/