Hey there.
I am working on a code that allows for several rows to be echoed on the page once specific rows are inserted in the database.
I will be posting the main code and then explaining each code snippet as we move on in order to clarify the problem at the end.
Full code :
[code=php]
<?php
require ‘connect.php’;
$usercookie = $_COOKIE[“username”];
$query = “SELECT * FROM chat”;
$result = $conn->query($query);
$row = mysqli_fetch_assoc($result);
if ($row[“USERNAME_OF_USER_TO”] == $usercookie) {
$sql = “SELECT DISTINCT USERNAME_OF_USER_FROM FROM chat WHERE USERNAME_OF_USER_TO = ‘$usercookie’ “;
$result = $conn->query($sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$recentchatuser = $row[“USERNAME_OF_USER_FROM”];
?>
<div id = “UserLinkSideMenu”>
<a href = “index.php?action=chat&user=<?php echo $recentchatuser; ?>”>
<table>
<tr>
<td>
<?php
$query = mysqli_query($GLOBALS[“___mysqli_ston”], “SELECT * FROM users WHERE `Username` = ‘$recentchatuser'”);
$row = mysqli_fetch_assoc($query);
$recentchatuserid = $row[“ID”];
$imgname = $row[“PimageName”];
if ($row[“PimageName”] === “Unkown” ) {
?>
<IMG src = “upload/Signalchatdefaultprofilepic.jpg” height = 50 width = 50 style = “border-radius: 25px;”></IMG>
<?php
} else {
?>
<IMG src = “upload/<?php echo $row[‘PimageName’]; ?>” height = 50 width = 50 style = “border-radius: 25px;”></IMG>
<?php
}
?>
</td>
<td></td>
<td>
<b style = “padding: 20px;”><strong><?php echo $recentchatuser; ?></strong></b>
</td>
</tr>
</table>
</a>
<a href = “userprofile.php?user=<?php echo $recentchatuser; ?>”>
Profile page
</a>
<hr>
</div>
<?php
}
}
} else {
$sqls = “SELECT DISTINCT USERNAME_OF_USER_TO FROM chat WHERE USERNAME_OF_USER_FROM = ‘$usercookie’ “;
$results = $conn->query($sqls);
if (mysqli_num_rows($results) > 0) {
while($row = mysqli_fetch_assoc($results)) {
$recentchatusers = $row[“USERNAME_OF_USER_TO”];
?>
<div id = “UserLinkSideMenu”>
<a href = “index.php?action=chat&user=<?php echo $recentchatusers; ?>”>
<table>
<tr>
<td>
<?php
$query = mysqli_query($GLOBALS[“___mysqli_ston”], “SELECT * FROM users WHERE `Username` = ‘$recentchatusers'”);
$row = mysqli_fetch_assoc($query);
$recentchatuserid = $row[“ID”];
$imgname = $row[“PimageName”];
if ($row[“PimageName”] === “Unkown” ) {
?>
<IMG src = “upload/Signalchatdefaultprofilepic.jpg” height = 50 width = 50 style = “border-radius: 25px;”></IMG>
<?php
} else {
?>
<IMG src = “upload/<?php echo $row[‘PimageName’]; ?>” height = 50 width = 50 style = “border-radius: 25px;”></IMG>
<?php
}
?>
</td>
<td></td>
<td>
<b style = “padding: 20px;”><strong><?php echo $recentchatusers; ?></strong></b>
</td>
</tr>
</table>
</a>
<a href = “userprofile.php?user=<?php echo $recentchatusers; ?>”>
Profile page
</a>
<hr>
</div>
<?php
}
}
}
?>
On the website i have built, i noticed that this part works fine :
[code=php]
$query = “SELECT * FROM chat”;
$result = $conn->query($query);
$row = mysqli_fetch_assoc($result);
if ($row[“USERNAME_OF_USER_TO”] == $usercookie) {
$sql = “SELECT DISTINCT USERNAME_OF_USER_FROM FROM chat WHERE USERNAME_OF_USER_TO = ‘$usercookie’ “;
$result = $conn->query($sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$recentchatuser = $row[“USERNAME_OF_USER_FROM”];
?>
<div id = “UserLinkSideMenu”>
<a href = “index.php?action=chat&user=<?php echo $recentchatuser; ?>”>
<table>
<tr>
<td>
<?php
$query = mysqli_query($GLOBALS[“___mysqli_ston”], “SELECT * FROM users WHERE `Username` = ‘$recentchatuser'”);
$row = mysqli_fetch_assoc($query);
$recentchatuserid = $row[“ID”];
$imgname = $row[“PimageName”];
if ($row[“PimageName”] === “Unkown” ) {
?>
<IMG src = “upload/Signalchatdefaultprofilepic.jpg” height = 50 width = 50 style = “border-radius: 25px;”></IMG>
<?php
} else {
?>
<IMG src = “upload/<?php echo $row[‘PimageName’]; ?>” height = 50 width = 50 style = “border-radius: 25px;”></IMG>
<?php
}
?>
</td>
<td></td>
<td>
<b style = “padding: 20px;”><strong><?php echo $recentchatuser; ?></strong></b>
</td>
</tr>
</table>
</a>
<a href = “userprofile.php?user=<?php echo $recentchatuser; ?>”>
Profile page
</a>
<hr>
</div>
<?php
}
}
}
But in this part even though the rows are inserted i am only getting one result when i should get say 2 or 3 :
[code=php]
else {
$sqls = “SELECT DISTINCT USERNAME_OF_USER_TO FROM chat WHERE USERNAME_OF_USER_FROM = ‘$usercookie’ “;
$results = $conn->query($sqls);
if (mysqli_num_rows($results) > 0) {
while($row = mysqli_fetch_assoc($results)) {
$recentchatusers = $row[“USERNAME_OF_USER_TO”];
?>
<div id = “UserLinkSideMenu”>
<a href = “index.php?action=chat&user=<?php echo $recentchatusers; ?>”>
<table>
<tr>
<td>
<?php
$query = mysqli_query($GLOBALS[“___mysqli_ston”], “SELECT * FROM users WHERE `Username` = ‘$recentchatusers'”);
$row = mysqli_fetch_assoc($query);
$recentchatuserid = $row[“ID”];
$imgname = $row[“PimageName”];
if ($row[“PimageName”] === “Unkown” ) {
?>
<IMG src = “upload/Signalchatdefaultprofilepic.jpg” height = 50 width = 50 style = “border-radius: 25px;”></IMG>
<?php
} else {
?>
<IMG src = “upload/<?php echo $row[‘PimageName’]; ?>” height = 50 width = 50 style = “border-radius: 25px;”></IMG>
<?php
}
?>
</td>
<td></td>
<td>
<b style = “padding: 20px;”><strong><?php echo $recentchatusers; ?></strong></b>
</td>
</tr>
</table>
</a>
<a href = “userprofile.php?user=<?php echo $recentchatusers; ?>”>
Profile page
</a>
<hr>
</div>
<?php
}
}
}
Now i do not know much about while loops and i think that the problem is related to my query structure or so.
The idea of the website is to be a web based chatting app and in this section of the code i am selecting the rows in the table “chat” to echo the names of people who sent messages to other people.
But i am getting only one result when “usercookie” did not send the message or not getting at all but when “usercookie” aka the user did send the message he will be able to see all the wanted rows.
Hope we can sort it out and if you could please explain the actual problem instead of just posting the solution that would be great !
Thank you for reading.