...

View Full Version : NOT fetching variable inside IF statement



neerajkhatri
09-15-2011, 05:22 PM
<?php
mysql_connect("localhost","root","");
mysql_select_db("register");
global $a;
$a=$_GET['country'];//this i am getting from a query string
echo $a;//it is working here

function n()
{
global $a;
echo $a;//it is not showing any value here
$res=mysql_query("INSERT INTO user(country) VALUES('$a')");//it insert a blank value in the data base
if($res==1)
{
echo "query inserted";
}
if($res==0)
{
echo "query not inserted";
}}

if(isset($_GET['country'])&&isset($_REQUEST['submit'])){
n();
}
?>

mlseim
09-15-2011, 07:18 PM
Functions are for taking-in something, and returning something ...




<?php
mysql_connect("localhost","root","");
mysql_select_db("register");
global $a;
$a=$_GET['country'];//this i am getting from a query string
echo $a;//it is working here

function n($a)
{
$res=mysql_query("INSERT INTO user(country) VALUES('$a')");//it insert a blank value in the data base
if($res==1)
{
$response= "query inserted";
}
if($res==0)
{
$response= "query not inserted";
}
return $response;
}

if(isset($_GET['country'])&&isset($_REQUEST['submit'])){
echo n($a);
}
?>




If you have to echo stuff inside a function, then don't use it as a function.
That sort of defeats the whole purpose of it.


.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum