0

HELP!!! I can't figure out why I am getting an error: no database selected. My code is below:

session_start();

$db_host        = 'localhost';
$db_user        = 'username';
$db_pass        = 'password!';

/* End config */

$link = mysql_connect($db_host,$db_user,$db_pass) or die('Unable to establish a DB     connection');

mysql_select_db("database", $link);

mysql_query("INSERT INTO table (field1, field2) 
VALUES ('$field1', '$field2')");

if(mysql_affected_rows($link)==1) { 

    echo "success";
}
else 
{
    echo "Error: ". mysql_error() ."";
}
5
  • 2
    Don't use the mysql_* functions; they are deprecated. Use PDO or MySQLi. Commented Jul 7, 2014 at 18:42
  • 1
    Is ur database name is 'database'?? Commented Jul 7, 2014 at 18:44
  • @Bhushya no, i changed it to that for privacy purposes when i posted it. Commented Jul 7, 2014 at 18:45
  • Then chk ur using proper database credentials or not.. even used credentials has required permission to the db Commented Jul 7, 2014 at 18:48
  • @Bhushya That worked! It was the database credentials. I just upgraded to cpanel and didn't realize you had to take that extra step. Thank you! Commented Jul 7, 2014 at 18:55

1 Answer 1

2

Possible problems
1. Does "username" have access to "database" (PRIVILEGES)
2. Does "database" exist?

Side note - You should be using mysqli or PDO because mysql will be deprecated in upcoming releases

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

Comments

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.