0

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.

  • use + name of your DB didn't work? – Adrien Brunelat Apr 19 '16 at 18:11
  • 2
    [stop using MySQL as it is ***DEPRECATED*** and transfer over to MySQLi](http://stackoverflow.com/questions/1390607/how-could-i-change-this-mysql-to-mysqli) . *Or* PDO. [What does Deprecated mean?](https://en.wikipedia.org/wiki/Deprecation) – Martin Apr 19 '16 at 18:12
  • It's not the "right" way to fix it, but you could change $sql = "INSERT INTO naam (test) VALUES ('$value')"; to $sql = "INSERT INTO dbname.naam (test) VALUES ('$value')"; – user2278120 Apr 19 '16 at 18:13
  • 2
    [Little Bobby](http://bobby-tables.com/) says [your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard Apr 19 '16 at 18:14
  • 2
    Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Apr 19 '16 at 18:14

1 Answers1

2

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.

nl-x
  • 11,762
  • 7
  • 33
  • 61
  • ok, i fixed the initial problem but hi ran into another problem: Table 'a1466880_databas.test' doesn't exist. what does this mean? – Luca van Straaten Apr 19 '16 at 18:31
  • @LucavanStraaten You are not showing your (real) code, so I can't see where you are going wrong. The query you showed above `INSERT INTO naam (test) VALUES ('$value')` indicates you have a table called `naam`, and according to your select the database name is `test`. But the error you are showing me now indicates you are trying to do something with a table called `test` in a database called `a1466880_databas`. – nl-x Apr 19 '16 at 18:35