PHP mysql_list_dbs

darkpunkcalob

New Member
Messages
22
Reaction score
0
Points
0
I am having an issue with this script, when vist the page, it just loads forever.

What am i doing wrong?

PHP:
<?php
$host = "localhost";
$user = "user1234";
$password = "pass1234";

$connection = mysql_connect ($host, $user, $password) or die ('DB connection failed because: ' . mysql_error());
$list = mysql_list_dbs($connection) or die ('DB list failed because: ' . mysql_error());
while ($row = mysql_fetch_object($list)) {
     echo $row;
     echo "<br />";
}
?>
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
I am having an issue with this script, when vist the page, it just loads forever.
Are you sure it takes forever to load, as opposed to generating no visible content?

Don't use die when outputting HTML. Rather than the very outdated mysql driver, use PDO. Here's what it would look like

PHP:
<?php
try {
  // LocalDB isn't a standard class; keep reading for more.
  $db = LocalDB::connect();
  ?>
  <ul>
    <?php foreach ($db->query('SHOW DATABASES') as $dbName): ?>
      <li><?php echo $dbName[0]; ?></li>
    <?php endforeach; ?>
  </ul>
  <?php
} catch (PDOException $exc) {
   /* only admins should be shown raw MySQL error. Of course,
      only admins should see the list of database names.
    */
   echo $exc->getMessage();
}
?>

If any value in the statement varies (which is much of the time), you'd use PDO::prepare rather than PDO::query.

For example implementations of LocalDB, see (e.g.) "Re: Display all that would be secret while Mysql is broken", "Re:
PHP:
 MySQL and PHP[/URL]".
 
Last edited:

darkpunkcalob

New Member
Messages
22
Reaction score
0
Points
0
I am sure.

At any rate, I have eliminated the need for this script.

Thanks to all who helped.
 

pixelboy_95

New Member
Messages
4
Reaction score
0
Points
0
I once made a nice script for that. When you press a button it shows the databases with javascript :cool:
 
Top