Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    NOT fetching variable inside IF statement

    PHP Code:
    <?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();
    }
    ?>
    Last edited by Inigoesdr; 09-15-2011 at 05:39 PM.

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Functions are for taking-in something, and returning something ...

    PHP Code:

    <?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.


    .
    Last edited by mlseim; 09-15-2011 at 06:21 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •