我如何使此php代码连接到sql工作台?

问题描述 投票:0回答:2

我在使用pdo连接到Sql Workbench时遇到问题。尽管我尝试了各种连接方式,但仍显示以下错误。

收到错误:

SQLSTATE [HY000] [1045]用户'root'@'localhost'的访问被拒绝(使用密码:否)

<?php
session_start();
try {
$con = new PDO ("mysql:host=localhost;dbname=users",'root','');
if(isset($_POST['signup'])){
    $username = $_POST['username'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $city = $_POST['city'];

    $insert = $con->prepare("INSERT INTO users1(username,email,password,fname,lname,city)
            values(:username, :email, :password, :fname, :lname, :city)
            ");
    $insert->bindParam (':username',$username);
    $insert->bindParam (':email',$email);
    $insert->bindParam (':password',$password);
    $insert->bindParam (':fname',$fname);
    $insert->bindParam (':lname',$lname);
    $insert->bindParam (':city',$city);
    $insert->execute();
}elseif (isset($_POST['signin'])){
    $username = $_POST['username'];
    $password = $_POST['password'];

    $select = $con->prepare("SELECT*FROM users1 where username='$username' and password='$password'");
    $select->setFetchMode(PDO::FETCH_ASSOC);
    $select->execute();
    $data=$select->fetch();
    if($data['username']!=$username and $data['password']!=$password)
    {
        echo "Invalid username or Password";
    }
    elseif($data['username']==$username and $data['password']==$password)
    {
        $_SESSION['username']=$data['username'];
        $_SESSION['email']=$data['email'];
        $_SESSION['fname']=$data['fname'];
        $_SESSION['lname']=$data['lname'];

        header("location:profile.php");
    }
     }
}
catch (PDOException $e) { 
      echo "Error: ". $e -> getMessage();
}?>
php mysql pdo
2个回答
0
投票

我认为它要求输入密码

 $con = new PDO ("mysql:host=localhost;dbname=users",'root','your_password');

0
投票

如果您不知道密码,则可以使用以下命令来更新它:

UPDATE mysql.user SET Password=PASSWORD('MyNewPass') WHERE User='root';

//then connection will be:

$con = new PDO ("mysql:host=localhost;dbname=users",'root','MyNewPass');

谢谢,

© www.soinside.com 2019 - 2024. All rights reserved.