PDO选择不返回任何内容

时间:2016-10-07 11:21:05

标签: php mysql pdo

我正在尝试提取当前类别等于产品类别的项目列表,但是当我尝试下面的代码时,我得不到任何项目,尽管数据库中有6个条目,其中的类别为item等于当前类别名称

$category_name = $_GET['category']; 
    $category_products = $DB_con->prepare("SELECT * FROM products WHERE product_category = :category_name");
    $category_products->bindParam(":category_name",$category_name,PDO::PARAM_STR);   
    $category_products->execute();
    $row = $category_products->fetchAll(PDO::FETCH_ASSOC);
    var_dump($row);

1 个答案:

答案 0 :(得分:1)

您可以尝试稍微不同,如下面的轻微评论片段所示:

<?php

    // SANITIZE DATA FROM THE $_GET GLOBAL A BIT...
    $category_name      = isset($_GET['category']) ? htmlspecialchars(trim($_GET['category'])): null;
    $row                = null;

    // CHECK THAT $category_name EXISTS FIRST....
    if($category_name) {
        $parameters         = array("category_name" => $category_name);
        $category_products  = $DB_con->prepare("SELECT * FROM products WHERE product_category = :category_name");

        // YOU MAY TRY PASSING THE NAMED PARAMETERS DIRECTLY AS AN ARRAY TO THE execute METHOD:
        $category_products->execute($parameters);
        $row                = $category_products->fetchAll(PDO::FETCH_ASSOC);
    }
    var_dump($row);