Solved

Check database exists!

Posted on 2006-06-20
3
1,042 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

726 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