jeffjenx
jeffjenx

Reputation: 17457

PDO prepared statement not returning expected results

The prepared query returns the following resultset, when $this->show is set to saved-by-the-bell:

season
------
1
2
3
4

When I execute the following code:

$seasons = array( );

$query = $db->prepare(
  "SELECT `season` " .
  "FROM `tv` " .
  "WHERE `show_url` = ':show' " . 
  "GROUP BY `season` " .
  "ORDER BY `season` ASC;"
);

$query->bindParam( ':show', $this->show );
$query->execute( );
$query->setFetchMode( PDO::FETCH_OBJ );

while( $row = $query->fetch( ) )
{
  $season = new stdClass;
  $season->number = $row->season;
  $season->title = "Season {$row->season}";
  $season->url = $row->season;

  $seasons[] = $season;
}

return $seasons;

$seasons is an empty array, why?

$db is an instantiated object that extends PDO.

I've tried all kinds of debugging methods, but when I echo the rowCount, it says 0. I'm still new to PDO, but I can't seem to see what's wrong here.

Thanks in advance.

Upvotes: 1

Views: 854

Answers (1)

jeffjenx
jeffjenx

Reputation: 17457

Ahhh, I figured it out. I'll leave my stupidity up here, in case anyone else happens across this.

Apparently, when using prepared statements, you don't need to wrap your parameters in quotes in the SQL code.

So, the prepare statement should look like this:

$query = $db->prepare(
  "SELECT `season` " .
  "FROM `tv` " .
  "WHERE `show_url` = :show " . 
  "GROUP BY `season` " .
  "ORDER BY `season` ASC;"
);

Upvotes: 4

Related Questions