显示选择数据时出现问题

问题描述 投票:0回答:1

我的问题是显示数据。它多次重复相同的id。

<?php

$check_shared_section = mysqli_query($MYSQLi,"select * from `vp_wall_post` where `type` = 'section' and `username` = '".mysqli_real_escape_string($MYSQLi,$poster_username)."' ");

        $get_section = mysqli_fetch_array($check_shared_section);
        $section_post = trim(strip_tags($get_section["post"]));
        $section_page = trim(strip_tags($get_section["page_id"]));

        $section_id = trim(strip_tags($get_section["pid"]));

        echo $section_id;

 ?>

这些是我面临的问题。请帮我解决这个问题。它显示输出ID

e4zDFOL3jBgcH8YRfkzJ 
e4zDFOL3jBgcH8YRfkzJ 
e4zDFOL3jBgcH8YRfkzJ 

我想表现出不同的身份

e4zDFOL3jBgcH8YRfkzJ 
er556gdfg4asffgfgfgg
So2cLYtCTTMYD0fCNFjq
JGH63vAqIAnt5jNCH6OL

<?php

$check_shared_section = mysqli_query($MYSQLi,"select * from `vp_wall_post` where `type` = 'section' and `username` = '".mysqli_real_escape_string($MYSQLi,$poster_username)."' ");

        $get_section = mysqli_fetch_array($check_shared_section);
        $section_post = trim(strip_tags($get_section["post"]));
        $section_page = trim(strip_tags($get_section["page_id"]));

        $section_id = trim(strip_tags($get_section["pid"]));

        echo $section_id;

 ?>
php mysql
1个回答
0
投票

我不确定为什么你多次看到相同的pid值,因为你的代码中没有任何循环。但是,您没有得到所有不同的pid值的原因是您没有循环查询的结果。你需要在while的结果上使用mysqli_fetch_array循环,例如

while ($get_section = mysqli_fetch_array($check_shared_section)) {
    $section_post = trim(strip_tags($get_section["post"]));
    $section_page = trim(strip_tags($get_section["page_id"]));
    $section_id = trim(strip_tags($get_section["pid"]));
    echo $section_id;
    // do other stuff with the values
}

请注意,如果要在while循环之外进一步处理结果,则需要将值保存到数组中,例如

$section_posts = array();
$section_pages = array();
$section_ids = array();
while ($get_section = mysqli_fetch_array($check_shared_section)) {
    $section_posts[] = $section_post = trim(strip_tags($get_section["post"]));
    $section_pages[] = $section_page = trim(strip_tags($get_section["page_id"]));
    $section_ids[] = $section_id = trim(strip_tags($get_section["pid"]));
    echo $section_id;
    // do other stuff with the values
}
// you can use other loops to further process the values here
// e.g. foreach ($section_posts as $index => $section_post) {
// you can use $index to access the corresponding values from
// the $section_pages and $section_ids arrays e.g.
// $section_page = $section_pages[$index]
© www.soinside.com 2019 - 2024. All rights reserved.