1

I have no idea why this is not returning anything. I'll show the code and talk through the steps I've taken.

if (isset($_GET['observation'])) {
    require_once("../func/connect.php");
    $query = "SELECT * FROM observations WHERE option = ?";
    $stmt = $db->prepare($query);
    $stmt->bindValue(1, $_GET['observation']);
    $stmt->execute();

    $row = $stmt->fetch(PDO::FETCH_ASSOC);
    echo $row['question'];

} else {
    echo 'nope';
}

$row dumps a false boolean, $row['question'] is null.

I've wrote about a million queries and don't have a clue why this doesn't work.

Database table observations consists of id, question & option and the bindValue is correct to match a string in the database.

However, it returns null.

Quill
  • 2,729
  • 1
  • 33
  • 44
Danny123
  • 81
  • 1
  • 10
  • always add `$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );` and check out the error. `$row === false` means it failed – Kevin May 08 '15 at 10:57
  • @Ghost `Warning: PDOStatement::execute(): SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'option = 'obs_tld''` Thank you. However, the error doesn't make sense to me. – Danny123 May 08 '15 at 11:00
  • the correct answer is already below – Kevin May 08 '15 at 11:00

1 Answers1

3

option is a reserved word in mysql so you need to quote it with backticks:

$query = "SELECT * FROM observations WHERE `option` = ?";
jeroen
  • 91,079
  • 21
  • 114
  • 132
  • 1
    Ah, a backwards.. I always choose the best titles for my columns! thank you, I'll accept in5 mins – Danny123 May 08 '15 at 11:00