How can I make this a prepared Statements code?

Mark Deven

I was under the impression that this was a prepared statement script, but it appears I was wrong. How can I turn this into one? What is a prepared statement?

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

$sql = "SELECT Status FROM Users WHERE Username = ? AND Password = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ss", $_GET['username'], $_GET['password']);
$stmt->execute();
$result = $stmt->get_result();
$rows = $result->fetch_all(MYSQLI_ASSOC);

if ($result->num_rows > 0) {
// output data of each row or remove while loop if you wish
    while($row = $result->fetch_assoc()) {
        echo "<b style='color:green'>Found</b>";
    }
} else {
    echo "0 results";
}
$conn->close();
?>
kmdm

That's not using prepared statements. This is a basic example with no error/result checking:-

$sql = "SELECT Status FROM Users WHERE Username = ? AND Password = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ss", $_GET['username'], $_GET['password']);
$stmt->execute();
$result = $stmt->get_result();
$rows = $result->fetch_all(MYSQLI_ASSOC);

However you should consider selecting on username only and retrieving the password for comparison. You should also hash your passwords in the database if you're not doing so. Use php's password_hash() and password_verify() for that. The former would help in hashing the password while the latter would be used to verify if the posted password from the html form or original source matches the hashed password

the syntax is

password_hash($_GET['password'], PASSWORD_DEFAULT);

password_verify($_GET['password'], $hashedPasswordFromDatabase);

make sure the column storing the password is varchar(60) at least.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

PDO: How can I run multiple prepared statements in a transaction?

From Dev

How long can prepared statements be used?

From Dev

How long can prepared statements be used?

From Dev

How do I reuse prepared statements with QSqlQuery?

From Dev

How do I know if the prepared statements are cached?

From Dev

How Can I Assign the Results from Object Oriented Mysqli Prepared Statements

From Dev

What can I do to get actual prepared statements in Wordpress

From Dev

How are prepared statements implemented at code level and which entitity implements them?

From Dev

How can I make switch-case statements case insensitive?

From Dev

How can I make these PHP if statements more succinct?

From Dev

How do I retrieve an associative array with prepared statements?

From Dev

How can I make branchless code?

From Dev

How can i make a keypress action to this code?

From Dev

How can I make this Java Code Faster?

From Dev

How can I make this code if and else?

From Dev

How can I make this code tail recursive?

From Dev

how can I make the following code better?

From Dev

How can i make a keypress action to this code?

From Dev

How can I make this code more elegant?

From Dev

How can I make this code work properly?

From Dev

How can I make this JavaScript code cleaner?

From Dev

How to use prepared statements in Joomla?

From Dev

How to write prepared statements for this query?

From Dev

In bash, is there a way I can make shorter (if or) statements?

From Dev

How can I make this code into a class so I can reuse it?

From Java

Can prepared statements be enabled out of the box?

From Dev

Can prepared statements be enabled out of the box?

From Dev

How can I can make this JavaScript code cleaner?

From Dev

Can I put multiple prepared statements inside one stored procedure in MySQL?

Related Related

  1. 1

    PDO: How can I run multiple prepared statements in a transaction?

  2. 2

    How long can prepared statements be used?

  3. 3

    How long can prepared statements be used?

  4. 4

    How do I reuse prepared statements with QSqlQuery?

  5. 5

    How do I know if the prepared statements are cached?

  6. 6

    How Can I Assign the Results from Object Oriented Mysqli Prepared Statements

  7. 7

    What can I do to get actual prepared statements in Wordpress

  8. 8

    How are prepared statements implemented at code level and which entitity implements them?

  9. 9

    How can I make switch-case statements case insensitive?

  10. 10

    How can I make these PHP if statements more succinct?

  11. 11

    How do I retrieve an associative array with prepared statements?

  12. 12

    How can I make branchless code?

  13. 13

    How can i make a keypress action to this code?

  14. 14

    How can I make this Java Code Faster?

  15. 15

    How can I make this code if and else?

  16. 16

    How can I make this code tail recursive?

  17. 17

    how can I make the following code better?

  18. 18

    How can i make a keypress action to this code?

  19. 19

    How can I make this code more elegant?

  20. 20

    How can I make this code work properly?

  21. 21

    How can I make this JavaScript code cleaner?

  22. 22

    How to use prepared statements in Joomla?

  23. 23

    How to write prepared statements for this query?

  24. 24

    In bash, is there a way I can make shorter (if or) statements?

  25. 25

    How can I make this code into a class so I can reuse it?

  26. 26

    Can prepared statements be enabled out of the box?

  27. 27

    Can prepared statements be enabled out of the box?

  28. 28

    How can I can make this JavaScript code cleaner?

  29. 29

    Can I put multiple prepared statements inside one stored procedure in MySQL?

HotTag

Archive