use a variable in a function?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Snaked
    New Member
    • Mar 2008
    • 11

    use a variable in a function?

    [PHP]function flash_search()
    {
    $query="SELECT score FROM hi WHERE name='$sname'";
    $scre=mysql_que ry($query);
    $res=mysql_fetc h_array($scre,M YSQL_ASSOC);
    echo $sname;
    echo $res["score"];
    }[/PHP]
    This, by some crazy therory, should display the name and score of one person in my database.

    This is where $sname="Test" (an existing user)
    the database has 2 columns Name and Score. hi is the table's name.

    Why doesn't this work? I am only a novice in PHP...

    TA, Snaked
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    Since this is a function, did you define $sname as a global? If not: a function cannot address non-global variables. So you either pass the var to the function or you define it as global.

    Ronald

    Comment

    • Snaked
      New Member
      • Mar 2008
      • 11

      #3
      $sname=$_GET["name"];
      if that is what you mean, yes.

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        No, $_GET is global, $sname is not.

        Ronald

        Comment

        • Snaked
          New Member
          • Mar 2008
          • 11

          #5
          EDIT: Never mind, i fixed it myself. Whoops
          Last edited by Snaked; Apr 2 '08, 02:52 PM. Reason: FIX

          Comment

          • ronverdonk
            Recognized Expert Specialist
            • Jul 2006
            • 4259

            #6
            Originally posted by Snaked
            EDIT: Never mind, i fixed it myself. Whoops
            So how did you fix it. Can we share that?

            Ronald

            Comment

            • aktar
              New Member
              • Jul 2006
              • 105

              #7
              This post is for those who are seeking a solution to the same problem

              As Ronald stated earlier, if you're going to include an external variable inside a function then that variable must be declared as global. Heres how:

              [PHP]global $your_variable_ name;[/PHP]
              this will make your variable (and ofcourse its value) available inside the function.

              NOTE: you do not have to do this for _POST, _GET _REQUEST etc

              Comment

              • Markus
                Recognized Expert Expert
                • Jun 2007
                • 6092

                #8
                Originally posted by aktar
                This post is for those who are seeking a solution to the same problem

                As Ronald stated earlier, if you're going to include an external variable inside a function then that variable must be declared as global. Heres how:

                [PHP]global $your_variable_ name;[/PHP]
                this will make your variable (and ofcourse its value) available inside the function.

                NOTE: you do not have to do this for _POST, _GET _REQUEST etc
                And if you create a variable inside a function, declaring it as global will give you access to it outside of that function, no?

                Comment

                Working...