从表中选择回显一行而不是更多行

时间:2016-06-14 09:05:29

标签: php mysql echo

我遇到了问题,我希望得到你的帮助!

我已经建立了这个:

$stmt_select = $conn->prepare("SELECT * FROM `stores` WHERE `user_id`=$id;");
$stmt_select->execute();

while ($row_select = $stmt_select->fetch(PDO::FETCH_ASSOC)) {
    echo  '<ul class="dropdown-menu dropdown-alerts">
                <li>
                    <a href="#">
                        <div>
                            <i class="fa fa-home"></i> <strong>'. $row_select['area_store'] .'</strong>
                            <span class="pull-right text-muted small"><i>'. $row_select['name_store'] .'</i></span>
                        </div>
                    </a>
                </li>
            </ul>';        
};

我在表格中有两行,但echo只显示一个结果。有什么想法吗?

3 个答案:

答案 0 :(得分:1)

试试这段代码:

$stmt_select = $conn->prepare("SELECT * FROM `stores` WHERE `user_id`=$id;");
$stmt_select->execute();

$res = $stmt_select->get_result();

while ($row_select = $res->fetch_assoc()) {


          echo  '<ul class="dropdown-menu dropdown-alerts">
                 <li>
                 <a href="#">
                 <div>
                 <i class="fa fa-home"></i> <strong>'. $row_select['area_store'] .'</strong>
                 <span class="pull-right text-muted small"><i>'. $row_select['name_store'] .'</i></span>
                </div>
               </a>
              </li>
            </ul>';        
};

答案 1 :(得分:1)

你可以获取所有数据,而不是像这样进行foreach循环

$stmt_select = $conn->prepare("SELECT * FROM `stores` WHERE `user_id`=$id;");
$stmt_select->execute();
$stmt_select = $stmt_select->fetchAll(PDO::FETCH_ASSOC);

foreach ($stmt_select as $row) {
    echo  '<ul class="dropdown-menu dropdown-alerts">
                <li>
                    <a href="#">
                        <div>
                            <i class="fa fa-home"></i> <strong>'. $row['area_store'] .'</strong>
                            <span class="pull-right text-muted small"><i>'. $row['name_store'] .'</i></span>
                        </div>
                    </a>
                </li>
            </ul>';  
}

我也建议你在输入sql查询之前转义变量。

$stmt_select = $conn->prepare("SELECT * FROM `stores` WHERE `user_id`= :id");
$stmt_select->execute(["id" => $id]);
$stmt_select = $stmt_select->fetchAll(PDO::FETCH_ASSOC);

答案 2 :(得分:0)

试试这将有助于解决你的探测

  $stmt =$conn->prepare("SELECT * FROM stores where user_id=:user_id")  
  $stmt->bindValue(":user_id", $id);
  $stmt->execute();

  $results=$stmt->fetchall(PDO::FETCH_ASSOC);

  if(count($results) > 0 ){

    foreach ($row as $results) {

        echo  "<ul class=\"dropdown-menu dropdown-alerts\">
                <li>
                    <a href=\"#\">
                        <div>
                            <i class=\"fa fa-home\"></i> <strong>".$row['area_store'] ."</strong>
                            <span class=\"pull-right text-muted small\"><i>".$row['name_store'] ."</i></span>
                        </div>
                    </a>
                </li>
            </ul>";  

    }
  }elseif (count($results) <= 0) {
     echo "no records found";
  }

如果您发现这有用,请标记答案:)