Chris Sobolewski
Chris Sobolewski

Reputation: 12935

pdo select statement returning no rows

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.

Upvotes: 4

Views: 1232

Answers (2)

Your Common Sense
Your Common Sense

Reputation: 157872

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();
}

Upvotes: 0

Matthew
Matthew

Reputation: 48284

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

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

$_GET parameters automatically are url-decoded.

Upvotes: 3

Related Questions