I’m using this code to attempt to grab a table name and store it in a variable.
<?php
connectDB();
$sql = "SHOW TABLES";
$result = mysql_query($sql);
$tables = mysql_fetch_array($result);
foreach ($tables as $table) {
$table_name = $table[0];
echo $table_name;
}
closeConn();
?>
For one, its outputting ‘aa’ and ‘bb’ if i change the array index which i know arent table names in the db and two, what i want to do is run some code for every table in the db and insert the table name into a variable which i can use in said code? How would i do that?
mysql_fetch_arrayfetches one row, not the entire set. This means that when you do$table[0], you are actually working on the string value of each field in the row.You should put the
mysql_fetch_arrayinside the loop instead: