-1

table clients has 29 columns.

as an example, wi'll take five of them:

earth, sun, moon, jupiter, venus

row id=1 has the following values:

blue, gold, silver, lessons, arts

function giveme_vars($id){
    global $db;
    $sql = "select * from clients where id = :aid";
    $st = $db->prepare($sql);
    $st->execute([
        ":aid" => $id
    ]);
    $row = $st->fetch();
    // here I need something like this:  
    foreach($row as $key=>$val){
        $variable_key = $row_value;
        //then make all of them global;
    }
}

As a result I'm expecting:

$earth = 'blue';
$sun = 'gold';
$mon = 'silver';
$jupiter = 'lessons';
$venus = 'arts';

All of the variables should be global i.e. accessible for all files on the server.

Any help?

Shadow
  • 33,525
  • 10
  • 51
  • 64
qadenza
  • 9,025
  • 18
  • 73
  • 126
  • 1
    Possible duplicate of [How to declare a global variable in php?](https://stackoverflow.com/questions/13530465/how-to-declare-a-global-variable-in-php) – Shadow May 19 '19 at 15:13
  • 2
    You would be better off storing these values as an associative array rather than individual variables. – Nigel Ren May 19 '19 at 15:14
  • Possible duplicate of [Using braces with dynamic variable names in PHP](https://stackoverflow.com/questions/9257505/using-braces-with-dynamic-variable-names-in-php) – nice_dev May 19 '19 at 15:29

1 Answers1

-1

Like your first commentor pointed out: this post clarifies it

 class global{
        static $variable= "something";
    }

access and use:

function dosomething(){
    echo global::$var;
}
DigitalJedi
  • 1,577
  • 1
  • 10
  • 29
  • @qadenza. well yea ofc... maybe just try to be cooperative and use $GLOBALS["sun"] so you dont have to make 29 different global variables yourself. – DigitalJedi May 19 '19 at 15:41
  • I tried, but `$GLOBALS['sun']` is not accessible in another php file. – qadenza May 19 '19 at 15:45