Getting the value of variable variable in while looping


#1
id    mom          kid
(1)   earth        Asia
(2)   earth        Europe
(3)   Europe       France
(4)   Asia         Korea
(5)   earth        Africa
(6)   Asia         China
(7)   China        Peking
(8)   Europe       Germany
(9)   Germany      Berlin
(10)  Korea        Seoul
(11)  Seoul        Kangnam
(12)  Kangnam      Hak-dong
(13)  Hak-dong     Hak station
(14)  Asia         Japan
(15)  Hak station  1st floor
(16)  Kangnam      Seocho-dong

I have myTable like the above. and I have the code below.

$geo='Asia';
function getTableKids($mom) {
      $list=mysql_query("SELECT id, mom, kid FROM myTable0 WHERE mom='$mom'");
      while($rows=mysql_fetch_array($list)){$id=$rows['id']  $mom=$rows['mom']  $kid=$rows['kid']

             ${'mom'.$id}=$mom;  ${'kid'.$id}=$kid;

          echo $id.' '.$mom.' '.$kid.'[br]';
          getTableKids($rows['kid']);
      }
}

The result of the code above is following.

(4)    Asia          Korea
(10)   Korea         Seoul
(11)   Seoul         Kangnam
(12)   Kangnam       Hak-dong
(14)   Hak-dong      Hak station
(15)   Hak station   1st floor
(16)   Kangnam       Seocho-dong
(6)    Asia          China
(7)    China         Peking
(13)   Asia          Japan

The result above is so cool!!!

But I like to get some variables for using them from the code above
So I add some code “${‘mom’.$id}=mom; {‘kid’.$id}=$kid;” which is kind of variable variables like the below.

$geo='Asia';
function getTableKids($mom) {
      $list=mysql_query("SELECT id, mom, kid FROM myTable0 WHERE mom='$mom'");
      while($rows=mysql_fetch_array($list)){$id=$rows['id']  $mom=$rows['mom']  $kid=$rows['kid']

             ${'mom'.$id}=$mom;  ${'kid'.$id}=$kid;

          echo $id.' '.$mom.' '.$kid.'[br]';
          getTableKids($rows['kid']);
      }
}

echo $mom4.' '.$kid7;

I did echo “$mom4 and $kid7” expecting the result “Asia and Peking”

But I can’t get the value of the variables.
I hope I get the expecting result with your help?


#2

STOP WHAT YOUR DOING!

You are using dangerous, obsolete code that has been removed from PHP. You need to use PDO with Prepared Statements. This tutorial will get you going. https://phpdelusions.net/pdo

If you have that code online take it down IMMEDIATELY!


#3

Also, variable variables like you’re trying to use is a world of hurt. I wouldn’t go there.


#4

For me, variable variables are cool in a “Wow, PHP can do that?!” kind of way. But I’ve not found any practical use for them. I did write some code using them years ago when I was much a newbie, could not write complex queries, and didn’t have a good understanding of how to organize data.

IIRC, it was something like
SELECT countries FROM continents WHERE continent = $_POST['continent'];
then I looped through and got capital cities from an array. It was extremely hacky and though it was a fun exercise at the time it is not something I would do again unless I had absolutely no other choice.

If I’m understanding what your intended use for this is, I think assigning the row values to a multi-dimensional array or object would be much cleaner.


#5

With PDO you’d get an appropriate array quite easy:

$stmt = $pdo->prepare('SELECT id, mom, kid FROM myTable0 WHERE mom = ?');
$stmt->execute([$mom]);
$result = $stmt->fetchAll(PDO::FETCH_UNIQUE);

printf('%s %s', $mom, $result[7]['kid']);