如何让这个 php 代码连接到 sql 工作台? [复制]
Posted
技术标签:
【中文标题】如何让这个 php 代码连接到 sql 工作台? [复制]【英文标题】:How do I make this php code connect to sql workbench? [duplicate] 【发布时间】:2020-03-23 15:37:14 【问题描述】:我在使用 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();
?>
【问题讨论】:
【参考方案1】:我认为它要求输入密码
$con = new PDO ("mysql:host=localhost;dbname=users",'root','your_password');
【讨论】:
【参考方案2】:如果您不知道密码,可以使用以下命令更新密码:
UPDATE mysql.user SET Password=PASSWORD('MyNewPass') WHERE User='root';
//then connection will be:
$con = new PDO ("mysql:host=localhost;dbname=users",'root','MyNewPass');
谢谢,
【讨论】:
以上是关于如何让这个 php 代码连接到 sql 工作台? [复制]的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 PHP PDO 从 Mac 连接到 Sql Server?
Win 10 + IIS + PHP v7.1.7 + SQL Server 2008 R2如何让SQL Server连接正常工作?