-1

I made this code to display my user's details if I search for their email.

file.php

$sql = "SELECT * FROM users WHERE email='$email'";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $email);
$stmt->execute();
$result2 = $stmt->get_result();

file.html

while ($row = $result2->fetch_assoc()) { //results }

The problem with this code is that I get always a fatal error.

Fatal error: Uncaught Error: Call to a member function fetch_assoc() on null in ____ Stack trace: #0 {main} thrown in ______

Even though my script works perfectly because when I am searching for my users detail it shows them as expected.

What do they mean with this error? Can I get SQL Injected if I stay it like this? How can I remove this error?

Dharman
  • 30,962
  • 25
  • 85
  • 135
IchBinDuck
  • 19
  • 5
  • I am not sure how you got that error, but I assume you have error reporting silenced for mysqli. However, your real answer is here https://stackoverflow.com/questions/7537377/how-to-include-a-php-variable-inside-a-mysql-statement – Dharman Jan 04 '21 at 22:24
  • Show us the full code. Please create [mcve] and tell us whether you have `mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);` in your code – Dharman Jan 04 '21 at 22:25

1 Answers1

-1

1.) Fix binding your email parameter....

$sql = "SELECT * FROM users WHERE email='?'";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $email);
$stmt->execute();
$result2 = $stmt->get_result();

2.) For error you're getting i assume you get some error, you should check for $result2, if it's false > that means error

  • I appreciate your efforts to help out the community, but please note that we try to avoid answering low-quality questions. We already have answers on the site that we can offer as a solution and we don't need anymore low-quality questions answered. Even if you do, it's likely that such question will be removed in a couple days and your answer will be gone. Also, your answer contains an error. Please test your solutions before posting them. – Dharman Jan 04 '21 at 22:33