开发者

pdo select statement returning no rows

开发者 https://www.devze.com 2023-01-18 16:08 出处:网络
I am making a simple select from mySQL using PHP. I am doing something wrong that I can\'t seem to track down.

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(content开发者_StackOverflow中文版Title,' ' , '-') = :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.


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

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

$_GET parameters automatically are url-decoded.


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

精彩评论

暂无评论...
验证码 换一张
取 消