I made a table where I want to list some data. My goal is to list only 1 row if there are multiple rows with the same value. Let’s say I have 2 rows in MySQL table with the string “asd”. I want to see a single row, even if there are multiple rows with the same value.
<table>
<thead>
<th>#</th>
<th>Expeditor</th>
<th>Actiuni</th>
</thead>
<?php
$i=1;
$id2= $_SESSION["id"];
$result= mysqli_query($conn, "SELECT * FROM tb_user WHERE id= $id2");
$row = $result->fetch_assoc();
$userconn = $row['username'];
$rows= mysqli_query($conn, "SELECT * FROM mesajeuseri WHERE username='$userconn'");
$num=mysqli_num_rows($rows);
?>
<?php if($num == 1){ ?>
<?php foreach ($rows as $row): ?>
<tr>
<td><?php echo $i++; ?></td>
<td><?php echo $row["expeditor"] ?></td>
<td>
<a href="contact2.php?destinatar=<?php echo $row["expeditor"]; ?>">Raspunde</a>
</td>
</tr>
<?php endforeach; ?>
<?php } ?>
</table>
I tried to use $num as a counter, and if he hit 1 this should stop and print the result. This is how I think it should work. I know this is not secure and stuff, wide open to SQL, this is just an offline project for me to see how things work. With this condition if($num == 1)
I get 0 results in my table. If I change it to $num > 0 everything works fine.
This is my table:
table
userconn – represent the current user which is connected to the website and he should only see his messages not all of theme. For example if I’m connected with “dariu” I should see one row with the value “Lordikita” not 7 rows with the same value.