Forum Moderators: coopster

Message Too Old, No Replies

Show databases problem

Mysql

         

slimcharlie

5:31 pm on Dec 2, 2004 (gmt 0)

10+ Year Member



Hi I'm working with mysql and php. I need my users to be able to see the databases I have in the server.

I'm using show databases but I do not want them to see some of my databases.
Is there a way to give this instruction a path?

Thanks for all your help.

coopster

1:45 am on Dec 3, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, slimcharlie.

Path? No, not really. MySQL creates each database in a directory of it's own and the tables created in that database reside within that directory.

What version of MySQL are you running? As of MySQL 4.0.2, your users may see only those databases for which they have some kind of privilege, but that's only if they don't have the global SHOW DATABASES [dev.mysql.com] privilege.

Otherwise, you can specify a pattern:

SHOW DATABASES [LIKE 'pattern']

Lastly, you do have one other option, but it is based on you controlling the display of the tables via a PHP script. You could load an array with your table names that you don't want them to see, then during a php loop, compare the current table in your result set to see if it is in_array() [php.net] and if it is, don't display that particular table. This would obviously require you to maintain an array every time you added a new table, though.

coopster

9:15 pm on Dec 3, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Sorry, slimcharlie, I switched into table talk in that last paragraph when I meant databases. It doesn't matter, the same principle could apply to both. First, load up your array, then compare against it in a loop. An example:
<pre> 
<?php
$do_not_show = array('topsecret', 'patents', 'payroll');
$rows = mysql_query("SHOW DATABASES");
while ($row = mysql_fetch_assoc($rows)) {
if (!in_array($row['Database'], $do_not_show)) print $row['Database'] . "\n";
}
?>
</pre>