Solved

Check database exists!

Posted on 2006-06-20
3
1,037 Views
Last Modified: 2012-08-13
How can I use php to check an database name is exists or not!?

Thanks!
0
Comment
Question by:tunhien
3 Comments
 
LVL 36

Expert Comment

by:Zyloch
ID: 16944258
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
 
LVL 5

Accepted Solution

by:
aminerd earned 125 total points
ID: 16945029
Or..

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

echo 'Database '.($exists ? 'does' : 'does not').' exist.';
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 16945373
Alternatively change aminerds $exists line to:

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

-r-
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now