-1
$attacks_list = array();

function f1()
{     
    $sql = "SELECT * FROM user_pokemon_db WHERE user_id = '".$id."' AND pkmn_id = '".$pkmn_id."' " or die(mysql_error());
    $res = mysql_query($sql);
    $$attacks_list = mysql_result($res,0,"attacks");
    $attacks_list = unserialize($attacks_list);
    print_r($attacks_list);
 }    

I have already declared the array globally..!But still its not able to identify it! Is there any other way to declare it globally?

user3545779
  • 97
  • 2
  • 10
  • 1
    put `die` next to the `mysql_query` and remove `$` from this `$$attacks_list` and add `global $attacks_list` in function also – ɹɐqʞɐ zoɹǝɟ Apr 26 '14 at 08:36
  • possible duplicate of [Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?](http://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and) – deceze Apr 26 '14 at 08:53

1 Answers1

0

If you wish to use a global within a function, you must declare it to be global at function level too, e.g.

$attacks_list = array();

function f1()
{     
    global $attacks_list; // <-- ADDED
    $sql = "SELECT * FROM user_pokemon_db WHERE user_id = '".$id."' AND pkmn_id = '".$pkmn_id."' " or die(mysql_error());
    $res = mysql_query($sql);
    $$attacks_list = mysql_result($res,0,"attacks");
    $attacks_list = unserialize($attacks_list);
    print_r($attacks_list);
 }   

Have a look at the php.net article on variable scope for more information too: http://www.php.net/manual/en/language.variables.scope.php

slugonamission
  • 9,562
  • 1
  • 34
  • 41