Reputation: 1682
I tried to list all of my databases so I have used this:
$sql="SHOW DATABASES";
$query=mysql_query($sql,$connect);
then I fetch the db into an array, and I also listed the tables of each db from this array:
while ($row = mysql_fetch_assoc($query)){
$sql3="SHOW TABLES FROM `".$row['Database']."`"; //**i dont understand why $row['Database'] works?**
$query3=mysql_query($sql3, $connect);
}
Let's give an example array(db1,db2,db3). When I use:
SHOW TABLES FROM `".$row['Database'].
it will show each table from:
$row[0](db1) , $row[1](db2) , $row[2](db3)
Am I right? This is the theory behind this syntax? Or what?
Upvotes: 1
Views: 60
Reputation: 2916
You are accessing the array result by index name, if you make a print_r
of the variable $row you will get something like this:
Array
(
[Database] => information_schema
)
Array
(
[Database] => mysql
)
So you can access the value by using Database
index
EDITED:
In case of tables you will get something like this:
Array
(
[Tables_in_information_schema] => CHARACTER_SETS
)
...
Array
(
[Tables_in_mysql] => columns_priv
)
So the the index will change with every table, you can access it with something like this:
while ($row2 = mysql_fetch_assoc($query3)){
echo $row2["Tables_in_{$row['Database']}"];
}
OR
You can use mysql_fetch_row
instead, an access it using position:
while ($row2 = mysql_fetch_row($query3)){
echo $row2[0];
}
IMPORTANT:
I forgot to mention, but mysql_ extension is deprecated so you should use PDO or mysqli_ functions instead
Upvotes: 3
Reputation: 81988
I don't know the language you are using. But database queries typically return a resultsets, that allow you do access the values from different columns either by the index of the column or by it's name. The later is what you are doing here.
Upvotes: 0