Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Check database exists!

Posted on 2006-06-20
3
Medium Priority
?
1,057 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 500 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
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.

715 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