Luca van Straaten
Luca van Straaten

Reputation: 3

No database selected error mysql php

i am trying to connect my website to a database but i get a "No database selected" error, i tried finding a solution but i did not find anything. My code is:

    <?php

    define('DB_NAME', 'test');
    define('DB_UESR', '********');
    define('DB_PASSWORD', '********');
    define('DB_HOST', '********');

    $link = mysql_connect(DB_HOST, DB_UESR, DB_PASSWORD);

    if (!$link) {
        die('could not connect: ' . mysql_error());
    }

    $db_selected = mysql_select_db(DB_NAME, $link);

    if (!db_selected) {
        die('can\'t use name' . DB_NAME . ' : ' . mysql_error());
    }

    echo 'connected successfully ';

    $value = $_POST['naam'];

    $sql = "INSERT INTO naam (test) VALUES ('$value')";

    if (!mysql_query($sql)) {
        die('error: ' . mysql_error());
    }

    echo 'uploaded successfully ';

    ?>

the exact massage thad is displayed on the page is:

connected successfully error: No database selected

i hope someone can help me, i am new to this web site.

Upvotes: 0

Views: 344

Answers (1)

nl-x
nl-x

Reputation: 11832

You first need to change if (!db_selected) into if (!$db_selected) to see if you really select succesfully.

Also, you might want to check mysql_error() after selecting the database. It might be that you have insufficient privileges to select that database.

Afterwards, please check the sql injection problems that you have, as stated in the comments.

Upvotes: 2

Related Questions