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

ohtdti5x  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(239)

这个问题在这里已经有答案了

我可以在php中混合mysql api吗(4个答案)
两年前关门了。
所以,我写了一个代码,管理员可以查看用户的用户名和密码。我写了一些代码,从w3schools复制了一些:p。所以。每当我运行代码时,它都会显示错误[调用资源上的成员函数query()]
这是我的密码:
进程.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();
?>

索引.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;
    }

请更正我的密码,告诉我哪里错了。

ctehm74n

ctehm74n1#

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

<?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";
}

相关问题