简体   繁体   中英

Why mysql while returning just one result?

I've got this code for displaying comments (sorry for my last question).
It's included like this; all the variables are in another page.

<?php

include("scripts/connect_to_mysql.php");

$tab_kom = "SELECT * FROM komenty WHERE stat_id = '$zdni_id'  ORDER BY id DESC LIMIT 30";
$ukaz = '';
$res_kom = mysql_query($tab_kom);

$pocit = mysql_num_rows($res_kom);
if($pocit > 0) {
    while($row_kom=mysql_fetch_array($res_kom)) {
        $kom_uz_id = $row_kom['uid'];
        $kom_text = $row_kom['text'];
        $stat_id = $row_kom['stat_id'];
    }
    $tab_kom_uz = "SELECT * FROM uzivatele WHERE id = '$kom_uz_id' LIMIT 1";
    $res_kom_uz = mysql_query($tab_kom_uz);
    while($row_kom_uz=mysql_fetch_array($res_kom_uz)){
        $kom_uz_ids = $row_kom_uz['id'];
        $kom_uz_jm = $row_kom_uz['jmeno'];
    }
    $ukaz .=' '.$kom_text . ' ';
}else{
    $ukaz .= '';
}

?>

I should have two results with the same ID in the variable $zdni_id, but only one is showing. Do you know why?

while循环将覆盖数据,因此您将获得最后的结果,然后将其用于其余的IF

Try this

while($row_kom=mysql_fetch_array($res_kom)) {
    $kom_uz_id = $row_kom['uid'];
    $kom_text = $row_kom['text'];
    $stat_id = $row_kom['stat_id'];

    $tab_kom_uz = "SELECT * FROM uzivatele WHERE id = '".$kom_uz_id."' LIMIT 1";
    $res_kom_uz = mysql_query($tab_kom_uz);
    while($row_kom_uz=mysql_fetch_array($res_kom_uz)){
        $kom_uz_ids = $row_kom_uz['id'];
        $kom_uz_jm = $row_kom_uz['jmeno'];
    }
    $ukaz .=' '.$kom_text . ' ';        


}                

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM