PHP Tutorial: Print All MySQL Database Tables & Fields
" . $table[0] . " "; $i = 0; //row counter $row = mysql_query("SHOW columns FROM " . $table[0]) or die ('cannot select table fields'); while ($col = mysql_fetch_array($row)) { echo " Field Type Key Default Extra "; $i++; } //end row loop echo " " . $col[0] . " " . $col[1] . " " . $col[2] . " " . $col[3] . " " . $col[4] . "
"; } //end table loop ?>
Hi, Nice one. thanks. And with your permission i can get this details from my site. And give your reference link for this url.
Are you giving permission to do this. I have no more english language.
thanks,
J.
Sure you can use this on your website, just leave my domain name at the top of the code.
There is an additional column that can be shown. The updated listing is shown here.
/*
+………………………………………..+
: show table structure :
+………………………………………..+ */
$database = “test_db”;
$loop = mysqli_query($db, “SHOW tables FROM $database”) or die (‘cannot select tables’);
while($table = mysqli_fetch_array( $loop)) {
echo ”
” . $table[0] . ” table structure
Field
Type : Length
Key
Index
Default
Extra
“;
$i = 0; //row counter
$row = mysqli_query($db, “SHOW columns FROM ” . $table[0])
or die (‘cannot select table fields’);
while ($col = mysqli_fetch_array($row)) {
echo ”
” . $col[0] . ”
” . $col[1] . ”
” . $col[2] . ”
” . $col[3] . ”
” . $col[4] . ”
” . $col[5] . ”
“;
$i++;
} //end row loop
echo “”;
} //end table loop
Took pieces from this code, Thanks!
hi,
I was just wandering,
how many columns can we add?
how does the loop knows from $col ?
thx in adb=vance,
You can add up to count($col) columns to the list. If you always want to print all of them you can change the code inside of the while ($col = mysql_fetch_array($row)) to the following:
while ($col = mysql_fetch_array($row))
{
echo "<tr>";
//print the whole array returned in $col
for ($j = 0; $j < count($col); $j++)
echo "<td>{$col[$j]}</td>";
echo "</tr>";
$i++;
} //end row loop
thanx, it works nice,
That’s a great, cute script ! No need to go into cPanel / phpadmin anymore ! But now; Do you have any sample to print certain db-field contents just like an address book, such as this ?:
Name Surname Tel.no. e-mail address
————————————————————————————
Peter Baker 0123 45678 peter&baker.com
The script that I “created” prints it all, but not formatted, with certain distance between the fields. I don’t find a way how to format the headline and then underneath the content of the db equally. Sorry, PHP knows me perhaps, but I don’t know much about PHP….
Thanks for any advise,if you have
How can we display data of each table?
http://jadendreamer.wordpress.com/2011/02/09/php-tutorial-looping-through-displaying-a-result-set/
0) {
while ($row = mysqli_fetch_assoc($result)) {
echo ($row);
}
}
?>
it should be mysql not mysqli
above code fails; why? need help;
karma