douhuang75397
2018-01-31 15:49
采纳率: 0%
浏览 68
已采纳

我的任务是关于与xampp服务器的PHP连接,它没有连接并给我erroe [关闭]

Kindly show me the error in my code i am trying to connect the sign in form with database using php but i am getting these errors:

Warning: mysqli_connect(): (HY000/1045): Access denied for user 'root'@'localhost' (using password: YES) in C:\xampp\htdocs\MyProject\php\signin.php on line 9

Notice: Trying to get property of non-object in C:\xampp\htdocs\MyProject\php\signin.php on line 11

Fatal error: Uncaught Error: Call to a member function query() on boolean in C:\xampp\htdocs\MyProject\php\signin.php:17 Stack trace: #0 {main} thrown in C:\xampp\htdocs\MyProject\php\signin.php on line 17

<?php

$servername = "localhost";
$username = "root";
$password="";
$dbname = "myproject";

// Create connection
$conn = mysqli_connect($servername,$username,$password,$dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "INSERT INTO sign_in (username,password)";

if ($conn->query($sql) === TRUE) {
    echo "New record inserted successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}


$conn->close();

?>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dougou1943 2018-01-31 16:02
    已采纳

    You are getting this ERROR because of your is mixed up with mysqli_* functions and PDO code.

    Methods of both PDO connection and mysqli_* are different.

    Here is your error. ******Here is your error*****

    $sql = "INSERT INTO sign_in (username,password)";
    
    if ($conn->query($sql) === TRUE) { //ERROR line
        echo "New record inserted successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
    

    You have use function mysqli_query() instead of accessing member function of $conn, just take below correct example.

    // Perform queries 
    mysqli_query($conn,"SELECT * FROM Persons");
    mysqli_query($conn,"INSERT INTO Persons (FirstName,LastName,Age) VALUES ('Glenn','Quagmire',33)");
    
    mysqli_close($conn);
    

    Hope you get the point and will help you..

    点赞 打赏 评论
  • doufu2496 2018-01-31 15:53

    There must be a password on root mysql account that you're not providing here:

    $password="";
    

    Other parts of the error is a direct consequence of this, $conn became a boolean and not an object as the mysqli_connect() is unsuccessful (it gives back false on failure).

    点赞 打赏 评论
  • doupao5296 2018-01-31 15:54

    Your query is not correct, since no values where given to insert. It should look like this:

    $sql = "INSERT INTO sign_in (username,password) VALUES ($username,$password)";
    
    点赞 打赏 评论

相关推荐 更多相似问题