note 44855 added to function.key
| From: | brainz at mbnet dot fi | Date: | Wed, 18 Aug 2004 08:00:05 +0000 |
| Subject: | note 44855 added to function.key | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-74866@lists.php.net to get a copy of this message | ||
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;
}
----
Manual Page -- http://www.php.net/manual/en/function.key.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+44855
Delete -- http://master.php.net/manage/user-notes.php?action=delete+44855&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+44855&report=yes
Search -- http://master.php.net/manage/user-notes.php