tags:

views:

335

answers:

7

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

+1  A: 

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.

Zak
the same user can create database from command line. I get error "No database selected", nothing more.
perfectDay
A: 

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());
    }
}
Tom Haigh
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.
perfectDay
+1  A: 

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");
  }
Noah
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.
perfectDay
Sounds like your MySQL user doesn't have enough permissions.
staticsan
+3  A: 

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.

Clayton
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?
perfectDay
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?
Clayton
A: 

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.

Click Upvote
I don't see why that would be different ...
too much php
A: 

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 :)

roa3
A: 

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 ;)

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