Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

o227786060@gmail.com's avatar

PHP Beginners Section 3 episodes22

PHP Beginners Section 3 episodes22 $note = $db->query('select * from notes where id = :id', ['id' => $id])->fetch();

Seems like something is wrong here :id

Fatal error: Uncaught PDOException: 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 ':id'

0 likes
21 replies
Nakov's avatar

And further on Jeffrey is replacing the $id with $_GET['id'] from the global object. Did you tried that? Is the $id maybe null ? Because the :id in the query is just a wildcard in order to avoid SQL injection. You can try it this way too:

$note = $db->query('select * from notes where id = ?',  $id)->fetch();
1 like
Sinnbeck's avatar

Otherwise try

$note = $db->query('select * from notes where id = :id', [':id' => $id])->fetch();
1 like
Sinnbeck's avatar

@o227786060@gmail.com Does this work?

$note = $db->query('select * from notes where id = ?', [$id])->fetch();

If not, what is the exact error now ?

1 like
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@o227786060@gmail.com You are not passing in the parameters :)

function query($query, $params) //here
  {


      //PHP mysqli prepare();
      $statement = $this -> connection->prepare($query, $params); //here
   
      $statement -> execute();
      return $statement;
  }
}
5 likes
o227786060@gmail.com's avatar

Database.php file function query($query,$params =[]{ $statement = $this -> connection->prepare($query,$params); $statement -> execute($params);

3 likes
contreau's avatar

@o227786060@gmail.com If anyone is using MySQL 9.2.0 for this course, this solution works for the error in question!

1 like

Please or to participate in this conversation.