PHP代码无法正常工作,无法连接到mysql数据库

时间:2013-11-16 08:21:55

标签: php mysql localhost

我正在尝试通过php将简单的表单数据添加到mysql数据库中。

这是我的PHP代码..     

$dbc = mysqli_connect('localhost','root','sahunihal123321','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('Error querying database.');
mysqli_close($dbc);


  echo 'Thanks for submitting the form.<br />';
  echo 'You were abducted ' . $when_it_happened;
  echo ' and were gone for ' . $how_long . '<br />';
  echo 'Number of aliens: ' . $how_many . '<br />';
  echo 'Describe them: ' . $alien_description . '<br />';
  echo 'The aliens did this: ' . $what_they_did . '<br />';
  echo 'Was Fang there? ' . $fang_spotted . '<br />';
  echo 'Other comments: ' . $other . '<br />';
  echo 'Your email address is ' . $email;


?>

</body>
</html>

没有显示任何内容。

所以,连接阶段出了问题, 我在本地系统而不是服务器上工作,表单没有问题

我去访问我的mysql数据库,这就是我所看到的

nsnihalsahu@nsnihalsahu-home:~$ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 38
Server version: 5.5.34-0ubuntu0.13.10.1 (Ubuntu)

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> SHOW DATABASES
    -> ;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| aliendatabase      |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

mysql> USE aliendatabase
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> SHOW TABLES;
+-------------------------+
| Tables_in_aliendatabase |
+-------------------------+
| aliens_abduction        |
+-------------------------+
1 row in set (0.00 sec)

mysql> SELECT * FROM aliens_abduction;
Empty set (0.00 sec)

如何解决此问题?

4 个答案:

答案 0 :(得分:1)

如果您在同一服务器或本地实例中托管文件和数据库,请尝试使用主机名连接localhost。共振可能nsnihalsahu-home没有足够的私密性来建立与数据库的连接,您需要为该主机名提供凭证以建立主机名。 grant all privileges

$dbc = mysqli_connect('localhost','root','sahunihal123321','aliendatabase') 
      or die("Error " . mysqli_error($dbc));

有关特权的更多信息:http://dev.mysql.com/doc/refman/5.1/en/privileges-provided.html

您能否分享一下mysqli_error信息,以便我能更好地帮助您。

答案 1 :(得分:1)

我从php.net复制了这个,尝试使用它来连接到你的db:

对于远程:

$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
echo $mysqli->host_info . "\n";

对于localhost:

$mysqli = new mysqli("127.0.0.1", "user", "password", "database", 3306);
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

echo $mysqli->host_info . "\n";

答案 2 :(得分:0)

您的数据库连接在

中关闭代码mysqli_close($dbc)
$result=mysqli_query($dbc,$query)
or die('Error querying database.');
mysqli_close($dbc);

不是有条件的,即它会反正执行,从而导致你的数据库连接关闭。这里

if (!mysqli_query($link, "SET a=1")) {
    printf("Errormessage: %s\n", mysqli_error($dbc));
}

/* close connection */
mysqli_close($dbc);

代码中间删除mysqli_close($dbc) 使其成为条件。只应在出现错误时执行。

答案 3 :(得分:0)

我检查了我的错误日志,因为我在UBUNTU 13.10中这样做,每三个引用都变得很奇特。 这是一个PHP解析错误。使用了一个很好的代码编辑器,它刚刚离开,就个人而言,我正在转向Debian。