doulu6314 2014-07-14 05:23
浏览 80
已采纳

为什么我的PDO :: FETCH语句返回0?

Here is my code below:

$barcode = $_POST['barcode'];
$year = $_POST['year'];
$movietitle = $_POST['movietitle'];
$stmt = $pdo->prepare("SELECT COUNT(movietitle) FROM movies WHERE movietitle LIKE '%:movie%'");
$stmt->bindParam(':movie', $movietitle);
$stmt->execute();
$fetch = $stmt->fetch();
if($fetch[0] == 0) {    
    $displaytitle = $_POST['displaytitle'];
    $media = $_POST['media'];
    $youtube = $_POST['youtube'];
    $genre = $_POST['genre'];
    $youtube = "www.youtube.com/embed/" . $youtube;
    $stmtins = $pdo->prepare("INSERT INTO `movies` VALUES (null, :genre, :movietitle, :displaytitle, :year, :youtube, :media, :barcode)");
    $stmtins->bindParam(':genre', $genre);
    $stmtins->bindParam(':movietitle', $movietitle);
    $stmtins->bindParam(':displaytitle', $displaytitle);
    $stmtins->bindParam(':year', $year);
    $stmtins->bindParam(':youtube', $youtube);
    $stmtins->bindParam(':media', $media);
    $stmtins->bindParam(':barcode', $barcode);
    $stmtins->execute();
    $message = "The movie was added to your database. - $fetch[0]";
} else {
    $message = "Already owned.";
}

Every time it's run, $fetch[0] ALWAYS returns 0, despite the $stmt query returning more than 0. What's going on?

  • 写回答

1条回答 默认 最新

  • doupiai5597 2014-07-14 05:29
    关注

    I suggest you run PDO in exception mode...

    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    

    Then you would see the query error.

    The problem is, you can't use interpolate params like you have in your LIKE comparison. Try this instead...

    "SELECT COUNT(movietitle) FROM movies WHERE movietitle LIKE CONCAT('%', :movie, '%')"
    

    There's also an easier way to check for existence...

    $stmt = $pdo->prepare("SELECT 1 FROM movies WHERE movietitle LIKE CONCAT('%', :movie, '%')");
    $stmt->bindParam(':movie', $movietitle);
    $stmt->execute();
    if(!$stmt->fetch()) {
        // fetch will return false if there are no rows
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 (标签-python|关键词-char)
  • ¥15 python+selenium,在新增时弹出了一个输入框
  • ¥15 苹果验机结果的api接口哪里有??单次调用1毛钱及以下。
  • ¥20 学生成绩管理系统设计
  • ¥15 来一个cc穿盾脚本开发者
  • ¥15 CST2023安装报错
  • ¥15 使用diffusionbert生成文字 结果是PAD和UNK怎么办
  • ¥15 有人懂怎么做大模型的客服系统吗?卡住了卡住了
  • ¥20 firefly-rk3399上启动卡住了
  • ¥15 如何删除这个虚拟音频