/    Sign up×
Community /Pin to ProfileBookmark

obtain MySQL info.

After connecting to a MySQL server, can PHP return information about the MySQL server?
I’m looking for a way for PHP to echo back the names of each database contained on the MySQL server along with all the names of each table contained in each database.

Assuming that this is the code used to connect to the MySQL server:

[code]
<?
$link = mysql_connect(“xxxxx.xxxxxx.xxxxx:xxxxxxxxx”,”xxxxxxxxxx”,”xxxxxxxxxx”);
if(!$link){die(‘Connection:n<br>nFAILURE!’ . mysql_error());}
echo ‘Connection:n<br>nSUCCESSFUL!n’;
mysql_close($link);
?>
[/code]

How can I echo back a report similar to:

[code]
Connection:
SUCCESSFUL!

businesses
+table1
+table2

orginations
+table1

airports
+table1
+table2
+table3
+table4
[/code]

Extra credit if you come-up with a way for PHP to also echo the table format of each table.

Thanks in advance!

to post a comment
PHP

11 Comments(s)

Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYOct 06.2005 — do you mean something like that?
[CODE]
+---------+--------+-------+
| article | dealer | price |
+---------+--------+-------+
| 0003 | D | 1.25 |
| 0004 | D | 19.95 |
+---------+--------+-------+

[/CODE]
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYOct 06.2005 — here is a function that might interest you

http://www.php.net/mysql_list_fields
----


actually the function is deprecated
This function is deprecated. It is preferable to use mysql_query() to issue a [I]SQL SHOW COLUMNS FROM table [LIKE 'name'][/I] statement instead.[/QUOTE]
Copy linkTweet thisAlerts:
@UltimaterauthorOct 06.2005 — Thanks for the reply LiLcRaZyFuZzY!

Those functions should help me in the future however, I'm not concerned about the values of each field. What I meant by "table format" for my "extra credit" was e.g.
<i>
</i>business (
id int NOT NULL auto_increment primary key,
busname TEXT NOT NULL,
businfo TEXT NOT NULL,
contact TEXT NOT NULL
)

The above is the "table format" used to create a table like
<i>
</i>+------+----------+--------+--------+
|MEM ID|BUS NAME |BUS INFO|CONTACT |
+------+----------+--------+--------+
|000007|aplustv |repairs |Charles |
|000034|zenith |company |Jack |
|000002|mitsubishi|company |Bill |
+------+----------+--------+--------+

Let's say the name of this table is "table1" from the database "business"

Then the PHP program would echo a report like:
<i>
</i>business
+table1



It is possible to detect the table format of the MySQL database with PHP, yes?

Thanks for any futher assistance in advance.
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYOct 06.2005 — how do you print such a table?
[CODE]
+------+----------+--------+--------+
|MEM ID|BUS NAME |BUS INFO|CONTACT |
+------+----------+--------+--------+
|000007|aplustv |repairs |Charles |
|000034|zenith |company |Jack |
|000002|mitsubishi|company |Bill |
+------+----------+--------+--------+
[/CODE]


and i guess you should be able to get the column type, if it is what you are looking for
Copy linkTweet thisAlerts:
@NogDogOct 06.2005 — [code=php]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang='en'>
<head>
<META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=ISO-8859-1'>
<title>Database Info</title>
<style type="text/css">
<!--
body {
margin: 0;
padding: 1em;
font: medium arial, helvetica, sans-serif;
}
table {
border-collapse: collapse;
border: solid 2px black;
font-weight: normal;
font-size: small;
margin: 0.25em 0 0.5em 0;
}
td,th {
border: solid 1px black;
padding: 0.1em 0.25em;
}
li {
font-weight: bold;
font-size: large;
margin-top: 0.5em;
}
th { background-color: #ccc; }
h2 {
margin: 1em 0 0.5em 0;
border-top: solid 2px black;
padding-top: 1em;
}
-->
</style>
</head>
<body>
<h1>Database Info</h1>
<?php
$connx = mysql_connect('localhost', '******', '******') or
die("Unable to connect to MySQL");
$dbQuery = mysql_query("SHOW DATABASES") or die (mysql_error());
while($db = mysql_fetch_assoc($dbQuery))
{
$thisdb = mysql_select_db($db['Database']) or die (mysql_error());
echo "<h2>{$db['Database']}</h3>n";
echo "<ul>n";
$tabQuery = mysql_query("SHOW TABLES FROM {$db['Database']}") or
die(mysql_error());
while($table = mysql_fetch_assoc($tabQuery))
{
$tabKey = "Tables_in_{$db['Database']}";
$row = 0;
echo "<li>{$table[$tabKey]}";
$query = "DESC {$table[$tabKey]}";
$fldQuery = mysql_query($query) or die(mysql_error() . " -- $query");
echo "<table>n";
while($field = mysql_fetch_assoc($fldQuery))
{
if($row++ == 0)
{
echo "<tr>";
foreach($field as $key => $val)
{
echo "<th>$key</th>";
}
echo "</tr>n";
}
echo "<tr>";
foreach($field as $val)
{
echo "<td>$val</td>";
}
echo "</tr>";
}
echo "</li>";
}
echo "</ul>";
}
?>
</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@UltimaterauthorOct 06.2005 — Over time, the MySQL database would look like that after members leave and entries are deleted and edited etc.
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYOct 06.2005 — nogdog, where is the $db array defined in your code?
Copy linkTweet thisAlerts:
@UltimaterauthorOct 06.2005 — NogDog, thanks for the reply!

*tests script*

OMG! That is freaking awesome NogDog! I couldn't ask for a better program!

Even got the valid HTML and CSS decorating it ?
Copy linkTweet thisAlerts:
@UltimaterauthorOct 06.2005 — nogdog, where is the $db array defined in your code?[/QUOTE]
<i>
</i>while($db = mysql_fetch_assoc($dbQuery))


It works perfectly.

Thanks for all your help.
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYOct 06.2005 — 

cool script nogdog!
--


thx ultimater, saw that!
Copy linkTweet thisAlerts:
@NogDogOct 06.2005 — You're welcome. One of those interesting little challenges where I had to to a little research and some trial-and-error, so I learned something, too. ?
×

Success!

Help @Ultimater spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...