一个字段中的多重打印值

时间:2019-05-05 16:24:09

标签: php mysql

我如何从mysql打印值,我在表中有一些主题,我需要将它们打印在同一行作为注释。

但是代码在做重复项,我也在表中插入了注释。

$sql = "SELECT response.date,response.session, board.id as idboard, Jmeno,Koment,Text, Nazev as 'Nazev', Prezdivka as 'Prezdivka', board.Datum as 'Datel' FROM `board` left join users on board.ID_user=users.ID join response where response.ID_board=board.ID
";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<div class='card'>
  <div class='card-header' style='color:red;background-color:lightblue;'>" . $row["Nazev"]. " <a class='float-right'>Uživatel: " . $row["Prezdivka"]. "   Datum: " . $row["Datel"]. "</a></div>
  <div class='card-body' style='background-color:lightgrey;'>" . $row["Text"]. "</div> 
</div> 
<a href='prikazy/detail.php?id='>Odpovědi<a><br>  " . $row["Koment"].  " <a style='color:pink;'>" . $row["session"].  " " . $row["date"].  " </a> 
 <form method='post' action='index.php'> <input type='hidden' name='idboard' value=" . $row["idboard"]. " >  <input type='text' class='form-control float-right' placeholder='Zadejte nový komentář...' name='komentar'>    <br>
<BR><button type='submit' class='btn btn-info float-right' name='komentovat'>Komentovat</button>
</form><br><hr>";
    }
} else {
    echo "0 výsledků v DB";
}

2 个答案:

答案 0 :(得分:0)

我想问题出在您的查询中。因此,要做的第一件事就是清理它,使其可读。

SELECT 
  response.date,
  response.session, 
  board.id as idboard, 
  Jmeno,
  Koment,
  Text, 
  Nazev, 
  Prezdivka, 
  board.Datum as Datel 
FROM 
  board 
LEFT JOIN users ON board.ID_user = users.ID 
JOIN response 
WHERE 
  response.ID_board = board.ID

现在,我可以立即发现您的第二个JOIN缺少其ON部分。

这就是为什么您会重复的原因。

我不知道您的数据库结构,因此无法为您添加数据库。

请不要将查询放在一个长字符串中。将其以可读的格式放置,以便您可以对其进行调试,如下所示:

$myQuery = "SELECT 
              response.date,
              response.session, 
              board.id as idboard, 
              Jmeno,
              Koment,
              Text, 
              Nazev, 
              Prezdivka, 
              board.Datum as Datel 
            FROM 
              board 
            LEFT JOIN users ON board.ID_user = users.ID 
            JOIN response ON response.ID_board = board.ID";

答案 1 :(得分:0)

$sql = 
"
SELECT 
response.date,
response.session, 
board.id as idboard, 
Jmeno,Koment,Text, 
Nazev as 'Nazev', 
Prezdivka as 'Prezdivka', 
board.Datum as 'Datel' FROM `board` 
left join users on board.ID_user=users.ID 
join response where response.ID_board=board.ID 
";

我连接在一起的数据库的结构

 Table board (translated for you)
    id
    name
    text
    id_category
    id_user
    date

table response
id
comment
id_board
session_print_name
date

table users 

id
name
username
password
date
id_privilegies