• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1062
  • Last Modified:

Check database exists!

How can I use php to check an database name is exists or not!?

Thanks!
0
tunhien
Asked:
tunhien
1 Solution
 
ZylochCommented:
Hi tunhien,

A rather simple way would be to try and select it. If it is not there, it will not be selectable. For instance:

<?php

//...other code

$db_exists = @mysql_select_db("DB_NAME", $db_conn);

if ($db_exists === false)
{
    echo("DB does not exist.");
}

Regards,
Ted
0
 
aminerdCommented:
Or..

$result = mysql_query("SHOW DATABASES LIKE 'DB_NAME'", $link);
$exists = ($result && mysql_fetch_assoc($result);

echo 'Database '.($exists ? 'does' : 'does not').' exist.';
0
 
RoonaanCommented:
Alternatively change aminerds $exists line to:

$exists = ($result && mysql_num_rows($result) >0);

-r-
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now