SQL连接出错,一切看起来都不错?

时间:2017-02-01 21:12:26

标签: php mysql mysqli

我对SQL很新,所以请给我一些松懈(哈哈)。我尝试过以下脚本,但出于某种原因,它不起作用?我已经仔细检查了一切,这里的错误在哪里?

$conn = new mysqli('localhost','root','', 'hugh_network') or die("Error in the connect.." . mysqli_error($conn));

$sql = "INSERT INTO accounts (first, last, email, username, password, unid, grp)
VALUES ('$first', '$last', '$email', '$username', '$password', '$unid', '$grp')";

if ($conn->query($sql) === TRUE) {} else {
    die("An error has occured in the database connection. Contact an admin or developer.");
}

$conn->close();

1 个答案:

答案 0 :(得分:-1)

你的代码语法看起来很好,尝试将它包装在try / catch中以防万一我们错过了,并且首先逃避你的所有价值$ $ ....

try {

    $conn = new mysqli('localhost','root','', 'hugh_network') or die("Error in the connect.." . mysqli_error($conn));

    $sql = "INSERT INTO accounts (first, last, email, username, password, unid, grp) VALUES ('$first', '$last', '$email', '$username', '$password', '$unid', '$grp')";

    if ($conn->query($sql) === TRUE) {

    } else {
       die("An error has occured in the database connection. Contact an admin or developer.");
    }
    $conn->close();
  } catch (Exception $e) {
      die($e->getMessage());
  }

或检查数据库的完整性,列名,用户权限或类似内容......

取得联系;)