# Uncaught PDOException: SQLSTATE\[42000\]

**URL:** <https://www.phphelp.com/t/uncaught-pdoexception-sqlstate-42000/33403>\
**Category:** General PHP Help\
**Created:** [April 25, 2021, 12:21pm UTC](https://www.phphelp.com/t/uncaught-pdoexception-sqlstate-42000/33403 "2021-04-25T12:21:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sberry](https://www.phphelp.com/letter_avatar_proxy/v4/letter/s/5f8ce5/32.png) [@sberry](https://www.phphelp.com/u/sberry)\
**Post date:** [April 25, 2021, 12:21pm UTC](https://www.phphelp.com/t/uncaught-pdoexception-sqlstate-42000/33403/1 "2021-04-25T12:21:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Strider64](https://www.phphelp.com/user_avatar/www.phphelp.com/strider64/32/3161_2.png) [@Strider64](https://www.phphelp.com/u/Strider64)\
**Post date:** [April 25, 2021, 3:21pm UTC](https://www.phphelp.com/t/uncaught-pdoexception-sqlstate-42000/33403/2 "2021-04-25T15:21:37Z")

</div>

> [@sberry](#):
>
> ```auto
> // 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();
> 
> ```

Well you have a few boo-boos in the script

```auto
// 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](https://phpdelusions.net/pdo)

---

<div class="post-metadata">

**Author:** ![sberry](https://www.phphelp.com/letter_avatar_proxy/v4/letter/s/5f8ce5/32.png) [@sberry](https://www.phphelp.com/u/sberry)\
**Post date:** [April 26, 2021, 11:30am UTC](https://www.phphelp.com/t/uncaught-pdoexception-sqlstate-42000/33403/3 "2021-04-26T11:30:38Z")

</div>

Thanks for that.

Should I still include the filter and bindParam parts?

---

<div class="post-metadata">

**Author:** ![sberry](https://www.phphelp.com/letter_avatar_proxy/v4/letter/s/5f8ce5/32.png) [@sberry](https://www.phphelp.com/u/sberry)\
**Post date:** [April 26, 2021, 11:52am UTC](https://www.phphelp.com/t/uncaught-pdoexception-sqlstate-42000/33403/4 "2021-04-26T11:52:25Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![skawid](https://www.phphelp.com/letter_avatar_proxy/v4/letter/s/bbe5ce/32.png) [@skawid](https://www.phphelp.com/u/skawid)\
**Post date:** [April 26, 2021, 12:39pm UTC](https://www.phphelp.com/t/uncaught-pdoexception-sqlstate-42000/33403/5 "2021-04-26T12:39:37Z")

</div>

```php
    $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”_.

---

<div class="post-metadata">

**Author:** ![sberry](https://www.phphelp.com/letter_avatar_proxy/v4/letter/s/5f8ce5/32.png) [@sberry](https://www.phphelp.com/u/sberry)\
**Post date:** [April 26, 2021, 6:25pm UTC](https://www.phphelp.com/t/uncaught-pdoexception-sqlstate-42000/33403/6 "2021-04-26T18:25:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![skawid](https://www.phphelp.com/letter_avatar_proxy/v4/letter/s/bbe5ce/32.png) [@skawid](https://www.phphelp.com/u/skawid)\
**Post date:** [April 26, 2021, 8:52pm UTC](https://www.phphelp.com/t/uncaught-pdoexception-sqlstate-42000/33403/7 "2021-04-26T20:52:54Z")

</div>

“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.
