使用PHP和MySQL的基本但安全的登录系统有问题 我用的是Wamp, myown pc作为本地主机 港口:80 我已将php脚本文件保存在通常的默认位置 C:\wamp\www\webservice 我已经为php访问创建了一个配置文件,我称之为 config.inc.php 我已经用凭证username=dev创建了mysql数据库, 密码=根,数据库名=Web服务

使用PHP和MySQL的基本但安全的登录系统有问题 我用的是Wamp, myown pc作为本地主机 港口:80 我已将php脚本文件保存在通常的默认位置 C:\wamp\www\webservice 我已经为php访问创建了一个配置文件,我称之为 config.inc.php 我已经用凭证username=dev创建了mysql数据库, 密码=根,数据库名=Web服务,php,html,mysql,sql,validation,Php,Html,Mysql,Sql,Validation,它的代码如下:: 当我从浏览器运行时http://localhost/webservice/register.php 我在浏览器中将此错误显示为 无法连接到数据库:用户“dev”@“localhost”的SQLSTATE[28000][1045]访问被拒绝(使用密码:是) 请提供有关如何克服此问题的任何建议出于安全原因,请不要使用root。可能用户配置不好。也请查看此文档 此外,我在您的代码中看到,在脚本末尾调用了session_start(),如果之前发生了类似的错误,PHP将打印错误,而

它的代码如下::

当我从浏览器运行时
http://localhost/webservice/register.php


我在浏览器中将此错误显示为

无法连接到数据库:用户“dev”@“localhost”的SQLSTATE[28000][1045]访问被拒绝(使用密码:是)



请提供有关如何克服此问题的任何建议

出于安全原因,请不要使用root。可能用户配置不好。也请查看此文档

此外,我在您的代码中看到,在脚本末尾调用了session_start(),如果之前发生了类似的错误,PHP将打印错误,而不是启动会话,因此您应该在脚本开头使用它


希望有帮助。

这个错误是不言自明的。您输入了错误的凭据,或者未能正确配置数据库。看看PDO构造函数是如何使用的:尝试使用“root”作为用户名?
<?php 

    // These variables define the connection information for your MySQL database 
    $username = "dev"; 
    $password = "root"; 
    $host = "localhost"; 
    $dbname = "webservice"; 


    $options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'); 

    try 
    { 

        $db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options); 
    } 
    catch(PDOException $ex) 
    { 

        die("Failed to connect to the database: " . $ex->getMessage()); 
    } 
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);  
    $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);    
    if(function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) 
    { 
        function undo_magic_quotes_gpc(&$array) 
        { 
            foreach($array as &$value) 
            { 
                if(is_array($value)) 
                { 
                    undo_magic_quotes_gpc($value); 
                } 
                else 
                { 
                    $value = stripslashes($value); 
                } 
            } 
        }      
        undo_magic_quotes_gpc($_POST); 
        undo_magic_quotes_gpc($_GET); 
        undo_magic_quotes_gpc($_COOKIE); 
    } 
    header('Content-Type: text/html; charset=utf-8'); 
    session_start(); 
?>
<?php

require ("config.inc.php");

?>

<h1>Register</h1>
<form action="register.php" method="post">
    Username:<br/>
    <input type="text" name="username" placeholder="user name"><br/>
    Password:<br/>
    <input type="password" name="username" placeholder="user name"><br/>
    <input type="submit" value="Register user"/>
</form>