Solved

Check database exists!

Posted on 2006-06-20
3
1,048 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
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.
The viewer will learn how to count occurrences of each item in an array.
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…

623 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