0

Can anyone please help me, i'm going crazy and can't see what i'm doing wrong! I have this basic SELECT query:

$locationQuery = mysqli_query($conn, "SELECT * FROM uk_towns where county = 'Dyfed'");

This returns 1,000 results... Perfect!

Now, if I do this:

$county = "Dyfed";
$locationQuery = mysqli_query($conn, "SELECT * FROM uk_towns where county = '$county'");

I get nothing?

@Dharman It still doesn't like it. f I use the variable, I don't get a result. If I manually type it in, it returns what I expect but I know the variable holds the correct data. This is what I have now:

$county = $userDetails['county'];
// $county = "Dyfed";
$town = $userDetails['town'];

$sql = "SELECT * FROM uk_towns WHERE county=? AND name=?";
$stmt = mysqli_stmt_init($conn);
mysqli_stmt_bind_param($stmt, "ss", $county, $town);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);

while ($row = mysqli_fetch_assoc($result)) {
    echo $row['id'];
}
  • 2
    Use [prepared statements](https://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php) – Cid Nov 09 '20 at 11:17
  • If you enable error reporting then you will see that your second example likely throws an error [How to get the error message in MySQLi?](https://stackoverflow.com/a/22662582/1839439) – Dharman Nov 09 '20 at 11:18
  • You need to debug it further. I can't see anything wrong with your prepared statement – Dharman Nov 09 '20 at 12:15
  • If I echo both variables, I get the same value: "Dyfed". If I use either variable in the query, I get nothing. If I physically type it in, it works fine. – Origination Nov 09 '20 at 12:19
  • use `var_dump()` – Dharman Nov 09 '20 at 12:20
  • I feel such a twit. There was a blank space at the beginning that I thought I checked for and just didn't spot. Thank you SO so much! – Origination Nov 09 '20 at 12:54

0 Answers0