views:

29

answers:

2

I am making a simple select from mySQL using PHP. I am doing something wrong that I can't seem to track down.

Here's my statement:

$storyTitle = $_GET['title'];
$storyDate = urldecode($_GET['date']);
$SQL = "SELECT
        *
    FROM
        tblContent
    WHERE
        REPLACE(contentTitle,' ' , '-') = :storyTitle
    AND
        date(publishDate) = date(:storyDate)";
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
$q = $conn->prepare($SQL);
$q->execute(array(':storyTitle' => $storyTitle, ':storyDate' => $storyDate));
            while($r = $q->fetch()){
                 echo $SQL;
            };

This throws no errors and gives no rows.

If I replace the identifiers :storyTitle and :storyDate with a hard coded SQL statement, I get the correct results. I've stepped through and looked at the variables, and they seem right... I've already wasted so much time hunting, but I lack the expertise to pick out what I'm doing wrong.

+2  A: 

Dump the contents of your variables. I'm suspicious of:

$storyDate = urldecode($_GET['date']);

$_GET parameters automatically are url-decoded.

konforce
Led me to the solution even if incidentally. My variables were picking up extra quotations from earlier in the code. var_dump... I'll remember that for next time. :)
Chris Sobolewski
A: 

you have to ask PDO to throw an error explicitly

try {
  $conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
  $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

  $q = $conn->prepare($SQL);
  $q->execute(array(':storyTitle' => $storyTitle, ':storyDate' => $storyDate));
} catch (PDOException $e) {
  echo  $e->getMessage();
}
Col. Shrapnel
While this is good advice, it's not likely that his query would throw an exception since it is well formed. If anything, the call to `DATE()` would return `NULL` (given bad input) and no records would be returned.
konforce