0

There are several questions with similar titles, but all of them seem to have more complicated queries than what I am working with.

I'm trying to use a variable as part of a select query. The variable $theindex indicates the value for the field theindex which I am selecting for.

This is the code I am trying to use, which generates HTTP ERROR 500...

$sql_stmt = "SELECT * FROM products WHERE theindex='$theindex'";
$result = mysqli_query($conn,$sql_stmt);

This works...

$sql_stmt = "SELECT * FROM products WHERE theindex='1'";
$result = mysqli_query($conn,$sql_stmt);

Clearly the apostrophes around $theindex may be causing the problem, but all the approaches that I've tried thus far don't help and just generate HTTP ERROR 500. How can I use a variable in this select query?

Thank you for taking a look at this.

user586011
  • 1,908
  • 4
  • 18
  • 29
  • You should use query parameters. – GMB Sep 19 '19 at 21:30
  • 1
    The linked duplicate contains examples of how to use a parameter in a query for various database access technologies. Also, side note: HTTP Error 500 isn't the *real* error message. It's just the status code telling you that there *was* an error, not the actual message. Currently you're assuming information about that error, which may or may not be correct. Check your PHP logs, turn on error reporting, etc. to get the actual error message. – David Sep 19 '19 at 21:33
  • See also: https://stackoverflow.com/a/22662582/1839439 – Dharman Sep 20 '19 at 00:03

0 Answers0