-1

I'm trying to use a database 'coolometer. I've tried connecting like this:

$con = mysqli_connect("localhost", "x", "x", "coolometer") or die("cannot connect");

But it just says 'no database selected'. Any suggestions?

3
  • 1
    if mysqli fails it should output cannot connect. check in which line the error occur. Mar 21, 2013 at 6:50
  • 1
    Can you show us the condition in which you have show the message 'no database selected ?
    – Rikesh
    Mar 21, 2013 at 6:50
  • 1
    It's not a condition I've written, I think it must be a built-in error? I can't find it in my code!
    – Tiffany
    Mar 21, 2013 at 6:55

4 Answers 4

2

i think your MySQLi extension is disabled, so you need to first check var_dump(function_exists('mysqli_connect')); if it outputs bool(false), then you need to enable MySQLi extension.

1
  • I changed everything from mysqli_query to mysql_query and it worked.
    – Tiffany
    Mar 21, 2013 at 7:10
1

Check if your database name is correct, sometimes, there is a prefix in front of your databasename, and then the correct databasename to use is 'prefix_databasename'.

Another possibility is that your user x with password x doesn't have the correct access to the database you are trying to select. If that's the case, there's also no database selected.

3
  • The weird thing is that later on in my code it writes to the database and table correctly
    – Tiffany
    Mar 21, 2013 at 6:59
  • Later in your code? You have an or die(), the remainder of the code should not be executed if there is an error in connecting to the database... Can you copy the exact message you get?
    – Borniet
    Mar 21, 2013 at 7:03
  • It's okay, it's been fixed now
    – Tiffany
    Mar 21, 2013 at 7:30
0

Check if your databse is the same name... Seems that you simply write wrong your databse... Or mybe wrong user or password, sometimes if you are using connections to localhost who is on your pc(simpy home computer for testing sites) ther are no passord just left empty...(one of posibilty...

0

be sure that you didn't use

mysqli_close($con);

anywhere in the middle, where you need to use the $con connection later. because when you close it you can't use it unless you initiate the connection again.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.