简体   繁体   English

为什么mysql同时返回一个结果?

[英]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. 我应该在变量$ zdni_id中有两个具有相同ID的结果,但只显示一个。 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 . ' ';        


}                

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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