如何使用PDO Fetch方法从PHP中的数据库中获取特定的一组值?

时间:2018-06-19 20:10:48

标签: php html mysql database pdo

我有一个library.php,其中编写了函数,编写了特定的函数get_channels以获取属于customer_id的所有值。我正在使用PDO,我想在此函数中执行并获取值,结果必须显示在另一页的前端。但是此功能不会显示属于客户ID的表的任何名称。

library.php中的功能

public function get_channels($cust_id)
{
    try {
        $db = DB();
        $query = $db->prepare("SELECT * FROM sensor_table WHERE customer_id=:customer_id");
        $query->bindParam("customer_id", $cust_id, PDO::PARAM_STR);
        $query->execute();   
        return $query;
    } catch (PDOException $e) {
        exit($e->getMessage());
    }
}

前端网页中的代码

<div id="main-content">


            <?php 

            $count=$app->get_channels_count($customer->customer_id);
            $values=$app->get_channels($customer->customer_id);
            $channel_name=$values->fetch(PDO::FETCH_OBJ);
            echo $channel_name;
            for($i=0;$i<$values->rowCount();$i++)
            {

            ?>

            <div class="col-lg-3">
                        <div class="card alert">
                            <div class="products_1 text-center">
                                <div class="pr_img_price">
                                    <div class="product_img">
                                        <img src="assets/images/product_1/download.jpg" alt="" />
                                    </div>

                                </div>

                                <div class="product_details">
                                    <div class="product_name">
                                        <h4><?php echo $channel_name['table_name'];  ?></h4>
                                    </div>
                                    <div class="product_des">
                                        <p>Vimply dummy text the printing and type setting indust.</p>
                                    </div>
                                    <div class="prdt_add_to_cart">
                                        <button type="button" class="btn btn-primary btn-rounded  m-l-5">add to cart</button>
                                    </div>
                                </div>

                            </div>
                        </div>
                        <!-- /# card -->
                    </div>

                <?php
            }
            ?>

0 个答案:

没有答案