Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP错误:在一切正常时调用资源上的成员函数query()_Php_Mysql - Fatal编程技术网

PHP错误:在一切正常时调用资源上的成员函数query()

PHP错误:在一切正常时调用资源上的成员函数query(),php,mysql,Php,Mysql,所以,我刚刚编写了一个代码,管理员可以查看用户的用户名和密码。我写了一些代码,从w3schools复制了一些:p。 所以每当我运行代码时,它都会显示错误[调用资源上的成员函数query()] 这是我的密码: Process.php <?php $username = $_POST['uname']; $password = $_POST['pass']; $username = stripcslashes($username); $password = s

所以,我刚刚编写了一个代码,管理员可以查看用户的用户名和密码。我写了一些代码,从w3schools复制了一些:p。 所以每当我运行代码时,它都会显示错误[调用资源上的成员函数query()]

这是我的密码:

Process.php

<?php
    $username = $_POST['uname'];
    $password = $_POST['pass'];

    $username = stripcslashes($username);
    $password = stripcslashes($password);
    $username = mysql_escape_string($username);
    $password = mysql_escape_string($password);

    $conn = mysql_connect("localhost","root","");
    mysql_select_db("Login3");

    $result = mysql_query("select * from users where username = '$username' and password = '$password'");
    $row = mysql_fetch_array($result);

    if($row['username'] == $username && $row['password'] == $password) {
        echo "Welcome " . $row['username'];
    }
    else {
        echo "Invalid Credentials";
    }

    echo <h3>User List</h3>;

    mysql_connect("localhost","root","");
    mysql_select_db("Login3");

    $sql = "SELECT id, username, password FROM users";
    $request = $conn->query($sql);
    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
            echo "<br> id: ". $row["id"]. " - Name: ". $row["username"]. " " . $row["password"] . "<br>";
        }
    } else {
        echo "0 results";
    }

    $conn->close();
?>

Index.html

<!DOCTYPE html>
<html>
<head>
    <title>Login | Home</title>
</head>
<body>
    <form action="process.php" method="POST">
        <h3>Login</h3>
        Username: <input type="text" name="uname"><br><br>
        Password: <input type="password" name="pass"><br>
        <input type="submit" name="btn">
    </form>
    <br>
    <b>Test Accounts</b>
    <table>
        <tr>
            <th>Username</th>
            <th>Password</th>
        </tr>
        <tr>
            <td>admin</td>
            <td>admin@123</td>
        </tr>
    </table>
</body>
</html>
<style>

    table, th, tr, td {
        width: 25%;
        border-collapse: collapse;
    }
    th, td {
        border: 1px solid grey;
        text-align: center;
    }
    tr {
        background: white;
        transition-duration: 0.3s;
    }
    tr:hover {
        background: #ddd;
    }

登录|主页
登录
用户名:

密码:

测试帐户 用户名 密码 管理 admin@123 表,th,tr,td{ 宽度:25%; 边界塌陷:塌陷; } th,td{ 边框:1px纯灰; 文本对齐:居中; } tr{ 背景:白色; 过渡时间:0.3s; } tr:悬停{ 背景:ddd; }

请更正我的代码并告诉我哪里错了。

首先,您应该创建一次性数据库连接,并遵循正确的查询语法。若您想要访问查询方法,那个么您可以访问代码中不存在的mysqli类的查询方法,然后首先创建该类的intance,或者编写适当的查询syntex。我在下面核对一下

<?php
    $username = $_POST['uname'];
    $password = $_POST['pass'];

    $username = stripcslashes($username);
    $password = stripcslashes($password);
    $username = mysql_escape_string($username);
    $password = mysql_escape_string($password);

    $conn = mysql_connect("localhost","root","");
    mysql_select_db("Login3");

    $result = mysql_query("select * from users where username = '$username' and password = '$password'");
    $row = mysql_fetch_array($result);

    if($row['username'] == $username && $row['password'] == $password) {
        echo "Welcome " . $row['username'];
    }
    else {
        echo "Invalid Credentials";
    }
?>
<h3>User List</h3>
<?php
    mysql_connect("localhost","root","");
    mysql_select_db("Login3");

    $sql = "SELECT id, username, password FROM users";
    $request = mysql_query($sql);
    if (mysql_num_rows($request) > 0) {
    while($row = mysql_fetch_array($request)) {
        echo "<br> id: ". $row["id"]. " - Name: ". $row["username"]. " " . $row["password"] . "<br>";
    }
} else {
    echo "0 results";
}

用户列表

供参考。它们不再得到维护。看到了吗?相反,了解,并使用或-将帮助您决定哪一个最适合您。永远不要存储纯文本密码!请使用来处理密码安全性。如果您使用的PHP版本低于5.5,则可以使用密码\u hash()。在散列之前,请确保对它们进行了清理或使用了任何其他清理机制。这样做会更改密码并导致不必要的额外编码。好的。我了解一切,我会从下次开始处理,但现在有人能指出错误吗?你正在尝试混合mysql API。那不行。如果你把我的答案降级了,请写下评论,里面有什么问题。我的答案没有问题it@Lavish萨达纳:那为什么人们会贬低我的答案呢。