/    Sign up×
Community /Pin to ProfileBookmark

Problem with PHP while loop

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
}
}
}
?>
[/code]

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
}
}
}
[/code]

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
}
}
}
[/code]

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.

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogFeb 12.2016 — I didn't read through everything (I'm lazy), but I suspect you need a loop in the last code section where you do this:
[code=php]
$row = mysqli_fetch_assoc($query);
[/code]

The basic form for that is something like:
[code=php]
while(($row = mysqli_fetch_assoc($query)) != false) {
// at this point, use elements of $row to output stuff for that result row, e.g.:
echo "<li>{$row['some_column']}</li>n";
}
[/code]
Copy linkTweet thisAlerts:
@JadauthorFeb 13.2016 — The code you mentioned is related to the profile image ... i still tried your solution and it was of no use. Thanks any way for posting.
×

Success!

Help @Jad spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...