note 44855 deleted from function.key by aidan
| From: | aidan@php.net | Date: | Wed, 18 Aug 2004 11:05:27 +0000 |
| Subject: | note 44855 deleted from function.key by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-74875@lists.php.net to get a copy of this message | ||
Note Submitter: brainz at mbnet dot fi
----
Hi, I wanted to contribute with a couple of functions I've written that just might be handy for
someone. The first function (mysql_list_tables_array) returns the tables of a database as an array.
The second, more complicated one, returns every occurrence of a entered fieldname: which table and
what position in the table.
I'm pretty sure this can be made much easier, but as I couldn't find anything that works
the same way ... well here they are :)
function mysql_list_tables_array($dbname) {
$rs = @mysql_list_tables($dbname) or
die (mysql_error());
$a = array();
while ($row = @mysql_fetch_row($rs)) {
$a[] = $row[0];
}
}
function mysql_find_field($fname, $dbname) {
$tab = @mysql_list_tables_array($dbname);
$a = array();
do {
$result = @mysql_query("SHOW COLUMNS FROM ".current($tab));
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
$a+=array(current($tab) => null);
if (@mysql_num_rows($result) > 0) {
while ($row = @mysql_fetch_assoc($result)) {
$a[current($tab)][] = $row['Field'];
}
}
} while(next($tab));
$key = array();
do {
$key+=array(key($a) => null);
$key[key($a)] = array_keys(current($a), $fname);
if ($key[key($a)] == null) unset($key[key($a)]);
} while(next($a));
return $key;
}