2014-10-02 114 views
0

我已经在phpmyadmin中创建了一个数据库。当我提交表单时,脚本应该在数据库中插入数据。但是,我收到错误“没有选择数据库”。 你能看看我的代码,看看你是否能找到我出错的地方。谢谢没有数据库选择的错误php mysql

$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); 

回答

2

正确的语法是

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

阅读说明书: - http://in2.php.net/manual/en/function.mysqli-connect.php

对于本地主机没有密码

mysqli_connect('localhost','root', '', 'database_name'); 
+0

它没有密码 – user3602426 2014-10-02 11:57:20

+0

mysqli_connect('localhost','root','','database_name');保持密码为空 – 2014-10-02 12:00:12

+0

@ user3602426检查更新的答案 – 2014-10-02 12:01:20

0

你必须选择你的数据库mysqli_connect后使用mysqli_select_db('your_database_name');

+0

mysqli_select_db(connection,dbname); 我试过mysqli_select_db($ dbc,'aliendatabase');但它仍然表示没有选择数据库 – user3602426 2014-10-02 11:51:22