0

Why this code does not recognize the existing database?

$result = mysql_query("SHOW DATABASES LIKE $database", $conn);
echo $result."       aaaaaaaaaaaaaaaaaaa<br>";
if ($result) 
{
    echo ("Datebase <font color='#FF0000'><b> $database </b></font> already exist.<br /><br />");
}
else
{
    mysql_query("create database IF NOT EXISTS $database CHARACTER SET utf8 COLLATE utf8_general_ci");
    echo ("Datebase <font color='#FF0000'><b> $database </b></font> is succesfully created.<br /><br />");
}
3
  • Elaborate on 'not recognize'. Commented Feb 13, 2011 at 13:21
  • $database is my database name that I want to create or check if it exsists. Commented Feb 13, 2011 at 13:28
  • @Haim Evgi. Not true in this case you'll want an exact match, not a wildcard search. Commented Feb 13, 2011 at 13:31

4 Answers 4

1

This code make my somekinda sick...

Here is a good code:

<?php

$query = "SHOW DATABASES LIKE ". $database;
$mysqlquery = mysql_query($query);
if($mysqlquery){
    echo '<b>Database already exist.<br>';
}
else
{
    $createquery = "CREATE DATABASE IF NOT EXISTS ". $database ." CHARACTER SET utf8 COLLATE utf8_general_ci";
    $mysqlcreatequery = mysql_query($createquery);
    if($mysqlcreatequery){
        echo 'Database '. $database .' created. ';
    }
    else
    {
        echo 'Database is not created!';
    }
}
?>
Sign up to request clarification or add additional context in comments.

Comments

1

You forgot the quotes (always necessary when using strings in SQL) :

$result = mysql_query("SHOW DATABASES LIKE '$database'", $conn);

3 Comments

But why "create database IF NOT EXISTS $database CHARACTER SET utf8 COLLATE utf8_general_ci" without quotation works, but SHOW command does not?
Well in your example it's not really a string. MySQL waits for an identifier here, with a specific format (no spaces) and don't need quotes to know where your database name ends. You can have spaces in your database name, but in this case you need to use quotes. It's different when using LIKE since MySQL expect a string value type, which can potentially contains spaces.
Anyway just use quotes and it will work (or it would mean there is another error somewhere else)
0

As the pattern is a string, you'll have to quote it:

"SHOW DATABASES LIKE '$database'"

Comments

0

The database is a string as stated by Dr.Molle. Also, watch out for wildcards. An _ is a single character wildcard, so when you look for the database order_2010 it will also match orders2010, which will lead to the assumption that order_2010 is indeed an existing database.

Small chance, but I thought I should mention it. :)

2 Comments

Ok so what is the better way that checks if $database exists?
Escape the wildcards by replacing _ with \_ and % with \%.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.