Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Ksenia Breitenmoser
Ksenia Breitenmoser
20,874 Points

Error: SQLSTATE[HY000]: General error: 1 near ">": syntax error

Hey guys! Anyone can help to find a mistake? I am trying to filter my task list by date. Once I select a period and click run, I am getting the following mistake: Error: SQLSTATE[HY000]: General error: 1 near ">": syntax error

Here is my function:

function get_task_list($filter = null) {
    include 'connection.php';
    $sql = 'SELECT tasks.*, projects.title AS project FROM tasks
            JOIN projects ON tasks.project_id = projects.project_id';

  $where = '';

  if (is_array($filter)) {
      switch  ($filter[0]) {
          case 'project':
                $where = ' WHERE projects.project_id = ?';
                break;
          case 'category':
                $where = ' WHERE category = ?';
                break;
          case 'date':
                $where = ' WHERE date => ? AND date <= ?';
                break;

      }
  }
  $orderBy = ' ORDER BY date DESC';  
  if ($filter) {
      $orderBy = ' ORDER BY projects.title ASC, date DESC';
  }
    try {
    $results = $db->prepare($sql . $where . $orderBy);
        if (is_array($filter)) {
        $results->bindValue(1, $filter[1]);
            if ($filter[0] == 'date') {
                $results->bindValue(2, $filter[2], PDO::PARAM_STR);
            }
        }
  $results->execute();
    } catch (Exception $e) {
        echo "Error: " . $e->getMessage() . "</br>";
        return array(); //if array is returned then the foreach loop in respective place (project_list.php) is still valid and is not running the code;
    }
    return $results->fetchALL(PDO::FETCH_ASSOC);
}

and here is my reports.php file (a part of it related to Date optgroup):

<optgroup label="Date">
                        <option value="date:<?php
                        echo date('m/d/Y',strtotime('-2 Sunday'));
                        echo ":";
                        echo date('m/d/Y',strtotime('-1 Saturday'));
                        ?>">Last Week</option>
                        <option value="date:<?php
                        echo date('m/d/Y',strtotime('-1 Sunday'));
                        echo ":";
                        echo date('m/d/Y');
                        ?>">This Week</option>
                        <option value="date:<?php
                        echo date('m/d/Y',strtotime('first day of last month'));
                        echo ":";
                        echo date('m/d/Y',strtotime('last day of last month'));
                        ?>">Last Month</option>
                        <option value="date:<?php
                        echo date('m/d/Y',strtotime('first day of this month'));
                        echo ":";
                        echo date('m/d/Y');
                        ?>">This Month</option>
                    </optgroup>

Very much appreciated.

1 Answer

Bind the first parameter for date as string $results->bindValue(1, $filter[1]); Maybe you can introduce a condition like:

if (is_array($filter) && $filter[0] != 'date' ) { $results->bindValue(1, $filter[1]); if ($filter[0] == 'date') { $results->bindValue(1, $filter[1], PDO::PARAM_STR); $results->bindValue(2, $filter[2], PDO::PARAM_STR); }