简体   繁体   English

mysql数据库连接错误

[英]mysql database connectivity error

whenever I give sign up data and press sign up button data isn't inserted into the table and it says "failed". 每当我提供注册数据并按“注册”按钮时,数据就不会插入表中,并显示“失败”。 I m not getting why is that so. 我不明白为什么会这样。 HTML code is as follow. HTML代码如下。

<form action="connection.php" method="post">
<label for="fname" style="color: white; margin-top: 0%;">Full Name</label>
<input type="text" id="FulName" name="FullName" placeholder="Full Name">
<br>
<label for="email" style="color: white;">Email</label>
<input type="text1" id="Email" name="UserEmail" placeholder="User Email">
<br>
<label for="fname" style="color: white;">User Name</label>
<input type="text2" id="UserName" name="UserName" placeholder="User Name">
<br>
<label for="password" style="color: white;">Password</label>
<input type="Password" id="Pass" name="Pass" placeholder="User Password">
<br>                
<input type="submit" value="Sign Up">
</form> 

php Code is as follow. php代码如下。

define('DB_HOST', 'localhost');
define('DB_NAME', 'nfakonline');
define('DB_USER','root');
define('DB_PASSWORD','');
$con=mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect    MySQL: " . mysqli_error());
$db=mysqli_select_db(DB_NAME,$con) or die("Failed !" . mysqli_error());

Error: Failed 错误: 失败

在mysqli中,您必须将连接对象作为第一个参数传递

$db=mysqli_select_db($con,DB_NAME) or die("Failed !" . mysqli_error($con));

Change from

$con=mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect    MySQL: " . mysqli_error());

to

$con = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);

// Check connection
if (mysqli_connect_errno())
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

Simple Google search will tell you that mysqli_select_db() takes first parameter as mysql connection link and second argument as dbname. 简单的Google搜索将告诉您mysqli_select_db()将第一个参数作为mysql连接链接,将第二个参数作为dbname。

In your case it would be: 您的情况是:

mysqli_select_db($con, DB_NAME);

Also mysqli_error() takes connection link: 另外mysqli_error()带有连接链接:

mysqli_error($con);

and here is the link for you, you can read more: 这是您的链接,您可以阅读更多:

http://php.net/manual/en/mysqli.select-db.php http://php.net/manual/zh/mysqli.select-db.php

http://php.net/manual/en/mysqli.error.php http://php.net/manual/zh/mysqli.error.php

You did 2 mistakes: 您犯了2个错误:

  1. You used a wrong syntax for mysqli_select_db(DB_NAME,$con) . 您对mysqli_select_db(DB_NAME,$con)使用了错误的语法。

    Correct syntax is: mysqli_select_db($con,DB_NAME) . 正确的语法是: mysqli_select_db($con,DB_NAME)

  2. You didn't pass the connection in mysqli_error() function. 您没有在mysqli_error()函数中传递连接。

    Correct Syntax is: mysqli_error($con) 正确的语法是: mysqli_error($con)

Use this PHP code: 使用此PHP代码:

<?php
define('DB_HOST', 'localhost');
define('DB_NAME', 'mywebsite');
define('DB_USER','root');
define('DB_PASSWORD','');
$con=mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect    MySQL: " . mysqli_error());
$db=mysqli_select_db($con,DB_NAME) or die("Failed !" . mysqli_error($con));
?>

I hope, it will help you! 希望对您有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM