I need to write some code that cycles through the entire list of tables I have in my database, so first I need to get the list of all tables and add them to an array.
I'm trying to do this using this code:
include('./db.php');
$dataList = array();
$showTable = "SHOW TABLES from databaseA";
$getData = mysqli_query($con, $showTable);
while ($row = mysqli_fetch_row($getData)) {
$dataList[] = $row;
print_r($row);
}
My results are showing as:
Array
(
[0] => Array
(
[0] => Dave
)
[1] => Array
(
[0] => Bob
)
[2] => Array
(
[0] => Chris
)
[3] => Array
(
[0] => Matt
)
}
but if I use echo $dataList[0];
or echo $dataList[1];
I just get the word array
by itself.
I'm not sure what I'm doing wrong here...
I then need to build a php for loop utilizing this, and my initial thought was to do something like this:
for ($i=0; $i <= count($dataList); $i++) {}
but that's not working since I'm defining the array incorrectly....
You're building an array-of-arrays:
$data[] = $row;
^---array
^---push onto another array
You need
$data[0][0] -> Dave
$data[1][0] -> Chris
etc...
a var_dump($data)
would've shown you this. Echoing out an array in a string context will give you the word Array
- PHP won't stringify the array for you.
If it echos the word array
then that means it is an array.
Try
print_r($dataList);
This will probably be an array of arrays.
Move the print_r
outside the the While
statement and print the $datalist
instead of $row
include('./db.php');
$dataList = array();
$showTable = "SHOW TABLES from databaseA";
$getData = mysqli_query($con, $showTable);
while ($row = mysqli_fetch_row($getData)) {
$dataList[] = $row;
}
print_r($dataList);
@Marc B are right. But I want show some other way (may be, trick):
$dataList = array_reduce($dataList, 'array_merge', array());
foreach($dataList as $name){
echo $name;
}
// Dave
// Bob
// Below
In code above - "unwrapping first-level array" in 2D-array.