0

i have created a database in phpmyadmin. When I submit the form, the script is supposed to insert data in the database. However, i get the error "No database selected". Could you please look at my code and see if you can find where I went wrong. Thank you

    $dbc = mysqli_connect('localhost','aliendatabase') or die('Error connecting to MySQL      server.');
    $query = "INSERT INTO aliens_abduction (first_name, last_name, when_it_happened, how_long, ".
   "how_many, alien_description, what_they_did, fang_spotted, other, email) " .
   "VALUES ('$first_name', '$last_name', '$when_it_happened', '$how_long', '$how_many', " .
   "'$alien_description', '$what_they_did', '$fang_spotted', '$other', '$email')";


   $result = mysqli_query($dbc, $query)
   or die(mysqli_error($dbc));

   mysqli_close($dbc);
0

2 Answers 2

2

Correct Syntax is

mysqli_connect('host','username', 'password', 'database_name');

Read manual :- https://www.php.net/manual/en/function.mysqli-connect.php

For localhost without password

mysqli_connect('localhost','root', '', 'database_name');
Sign up to request clarification or add additional context in comments.

1 Comment

mysqli_connect('localhost','root', '', 'database_name'); keep password blank
0

You have to select your database after mysqli_connect using mysqli_select_db('your_database_name');

1 Comment

mysqli_select_db(connection,dbname); I tried mysqli_select_db($dbc, 'aliendatabase'); but it still says no database selected

Your Answer

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