Uncaught PDOException: SQLSTATE[42000]

I am trying to use a menu link (index.php?) for my pages, so that when the link is selected the information from a database is added to the page. However, I get the following error message:

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 MariaDB server version for the right syntax to use near ‘’` WHERE id = .id’ at line 1 in C:\xampp\htdocs\testsite\index.php:24 Stack trace: #0 C:\xampp\htdocs\testsite\index.php(24): PDOStatement->execute() #1 {main} thrown in C:\xampp\htdocs\testsite\index.php on line 24

Here is the code:

<?php

          // query database
          $pdo_statement = $conn->prepare("SELECT * FROM pages'` WHERE id = .id");
          $id = filter_input(INPUT_GET, 'id',  FILTER_SANITIZE_NUMBER_INT);
          $pdo_statement->bindParam(':id', $id, PDO::PARAM_INT);
          $pdo_statement->execute();
          $result = $pdo_statement->fetchAll();
          ?>
          <!-- display data -->
          <?php
            if(!empty($result)) {
              foreach($result as $row) {
          ?>
            <td class="navItem"><a href="index.php?id=$row['id']">
              <?php echo $row["menuheader"]; ?>
            </a></td>
            <?php
          }
        }
        ?>

Any help will be appreciated.

Well you have a few boo-boos in the script

// query database
          $pdo_statement = $conn->prepare("SELECT * FROM pages  WHERE id=:id");
          $pdo_statement->execute(['id' => $_GET['id']);

A good resource in my opinion is the following : https://phpdelusions.net/pdo

1 Like

Thanks for that.

Should I still include the filter and bindParam parts?

Tried your code (again, thanks):

  <?php

          // query database
          $pdo_statement = $conn->prepare("SELECT * FROM pages WHERE id=:id")
          $pdo_statement->execute(['id' => $_GET['id']]);
          $result = $pdo_statement->fetchAll();
          ?>
          <!-- display data -->
          <?php
            if(!empty($result)) {
              foreach($result as $row) {
          ?>
            <td class="navItem"><a href="index.php?id=$row['id']">
              <?php echo $row["menuheader"]; ?>
            </a></td>
            <?php
          }
        }
        ?>
      </tr>

However I get an error message: Parse error: syntax error, unexpected ‘$pdo_statement’ (T_VARIABLE) in C:\xampp\htdocs\testsite\index.php on line 23

What can I do to solve this issue?

    $pdo_statement = $conn->prepare("SELECT * FROM pages WHERE id=:id")

You’ve missed a ; from the end of this line. PHP has tried to parse your code and seen the start of the next line before it expected to see it; hence “unexpected”.

Thanks. Did as you suggested:

$pdo_statement = $conn->prepare("SELECT * FROM pages WHERE id=:id");

However, I get the following error messages:

Notice: Undefined index: id in C:\xampp\htdocs\testsite\index.php on line 23

Notice: Undefined variable: row in C:\xampp\htdocs\testsite\index.php on line 178

Notice: Undefined variable: row in C:\xampp\htdocs\testsite\index.php on line 183

I looked into similar problems and have seen isset() used so was wondering if this will help, and if so how would I use it for the id?

Thanks

“undefined index” means the key you’re trying to get from an array doesn’t exist. If line 23 is the same as before, the array yuo’re looking in is $_GET and the key is "id".

$_GET is a keyed array of the parameters passed in the URL; I’d expect it to look something like www.mywebsite.com/page.php?id=123. If you’re getting this error, you’re not passing an ID. You need to decide how your code will behave if there’s no id passed, then you’ll use isset to make that happen.

The other two errors are from another part of your code, as shown by the line number.

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service