SELECT * from SQL table using prepared statement

后端 未结 4 1960
梦谈多话
梦谈多话 2021-01-13 22:00

I\'m using a prepared statement to SELECT * from a MySQL table and I\'m not sure how to use while($row = mysqli_fetch_array($stmt)) to loop through

4条回答
  •  广开言路
    2021-01-13 22:28

    Here is a simple example of a 'prepared select statement' and echoing out the result.

        if($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['uname']) && isset($_POST['pword'])) {
    
        $stmt = $conn->prepare("SELECT * FROM members WHERE username = ? && password = ?");// substitute '$conn' with whatever you named your database connection.
        $stmt->bind_param("ss", $username, $password);
        $username = $_POST['uname'];
        $password = $_POST['pword'];
        $stmt->execute();
        $result = $stmt->get_result();
        if ($result->num_rows ===0) exit('Not-A-Thing');
        while($row = $result->fetch_assoc()) { echo $row['id'] . "
    " . $row['username']; } $stmt->close(); }

    This is just an example. If you were searching for how to bind parameters, then you more than likely already know what the '?'s and 'ss's mean. No explanation should be needed if you have previously queried a database before.

提交回复
热议问题