0

I have this code:

               if(!mysql_connect($host,$user,$passwd)){
                    die("Hoops, error! ".mysql_error());
                }

...no error from here.

                if(!mysql_select_db($db,$connect)){
                    $create_db = "CREATE DATABASE {$db}";
                    mysql_query($create_db,$connect);
                    mysql_query("USE DATABASE {$db}",$connect);
                }

..."no database selected" error from here. I would like to select database if it exists and if doesn't then create it and select it.

Why is my code not right?

Thank you in advance

3
  • Are you sure $db is a valid database name? Commented Feb 12, 2009 at 0:39
  • yes I'm sure about it (its employers), I tried it also without variable and it still didn't work. Commented Feb 12, 2009 at 0:48
  • Whats with the braces? I haven't worked with mysql/php in ages, but I don't remember that at all. Commented Feb 12, 2009 at 2:01

7 Answers 7

3

Where are you saving the value returned by mysql_connect()? Don't see it here. I assume $host, $user, $password and $db are properly set ahead of time. But you're passing a param to mysql_select_db that may not be properly set.

$connect = mysql_connect($host,$user,$passwd);
if (!$connect) {
    die('Could not connect: ' . mysql_error());
}
if(!mysql_select_db($db,$connect)) ...

Start by checking to see if you can select without the CREATE query first. Try a simple SELECT query to start. If you can connect, select the db, and execute a SELECT query, that's one step. Then try the CREATE query. If that doesn't work, it's almost certainly a matter of permissions.

Sign up to request clarification or add additional context in comments.

2 Comments

My parameters are alright. I've got variable $connect set like you wrote here. Connection is without error, it just doesn't want to select db.I have no idea.Do you?
Can you perform a simple SELECT query on $db? Leave out the CREATE query to test. Do you get an error? If so, what mysql_errno is returned?
1

You might need database create permissions for the user attempting to create the database. Then you need to operate on a valid connection resource. $connect never looks to be assigned to the connection resource.

1 Comment

the same user can create database from command line. I get error "No database selected", nothing more.
1

Why not simply use the CREATE DATABASE IF NOT EXISTS syntax instead?

Something like this ...

  $con = mysql_connect('localhost');
  $sql = 'CREATE DATABASE IF NOT EXISTS {$db}';
  if (mysql_query($sql, $con)) {
    print("success.\n");
  } else {
    print("Database {$db} creation failed.\n");
  }
  if(!mysql_select_db($db,$connect)){
    print("Database selection failed.\n");
  }

2 Comments

I tried your code, but the return was failed and failed :(. variable $connect is ok, it doesn't return any error, so I really don't know where is the problem.
Sounds like your MySQL user doesn't have enough permissions.
0

You should check the return value of mysql_query() - currently if any of those calls fail you won't know about it:

if(!mysql_select_db($db,$connect)){
    if (!mysql_query("CREATE DATABASE $db", $connect)) {
        die(mysql_error());
    }

    if (!mysql_select_db($db, $connect)) {
        die(mysql_error());
    }
}

1 Comment

it's so weird, it doesn't report any error. It says "no database selected" only at the point when I want to insert data into table.
0

Change the line

mysql_query($create_db,$connect);
mysql_query("USE DATABASE {$db}",$connect);

To

mysql_query($create_db,$connect);
mysql_select_db($db);*

and it should work.

Comments

0

you could try w3schools website. They have a very simple and easy to learn tutorial for selecting database. The link is : http://www.w3schools.com/php/php_mysql_select.asp Hope this help :)

Comments

0

I would like to thank to all of you, however I found fault on my side. This script was in class and one of variables were not defined inside this class. So I'm really sorry. I don't know how to consider the right answer, but I noticed my mistake after reading Clayton's answer about not properly set parameters, so I guess he is the winner ;)

1 Comment

@perfectDay The good thing is you got the problem solved. I was beginning to wonder what happened with this one ... Thanks.

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.