Jump to content

PHP While Loop Help + Other Stuff


MrXkill

Recommended Posts

Hi! I am programming an alliance page for a game I am making and I'm having some trouble.

 

1. When the If ($aid == 0) statement is true, it prints out "Choose an Alliance," but doesn't list the top 5 in the database (the LIMIT 5 in the sql query).

2. When the else statement is true, the page is completely blank. Any ideas? Thanks in advance. Here's my code:

 

(located at top of page before html)

<?php
include_once('php/connect.php');
session_start();
?>

 

(located in a div)

<?php
$uid = $_SESSION['uid'];
$username = $_SESSION['username'];

$sql = "SELECT * FROM hack_wars_users WHERE uid='".$uid."' LIMIT 1";
$res = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($res) or die(mysql_error());

$aid = $row['aid'];
if ($aid == 0) {

$sql2 = "SELECT * FROM alliances WHERE aid >= '1' LIMIT 5";
$res2 = mysql_query($sql2) or die(mysql_error());
echo "<h1>Choose an Alliance</h1><br />";
	while ($row2 == mysql_fetch_assoc($res2) or die(mysql_error())) {
	$alliancename = $row2['name'];
	$alliancedes = $row2['description'];
	echo "Name: ".$alliancename." Description: ".$alliancedes."<br />";

}
Echo "<hr /><br /><h2>Search for an Alliance</h2><br />
<from action='php/alliancesearch.php' method='post'>Search: <input type='text' value='Alliance Name' name='name'> <input type='submit' value='Search' name='search'></form><br />";
} else {
$sql2 = "SELECT * FROM alliances WHERE aid='".$aid."'LIMIT 1";
$res2 = mysql_query($sql2) or die(mysql_error());
$row2 = mysql_fetch_assoc($res2) or die(mysql_error());
$alliancename = $row2['name'];
$alliancenews = $row2['news'];
$oid = $row['oid'];
$sql3 = "SELECT * FROM hack_wars_users WHERE uid='".$oid."' LIMIT 1";
$res3 = mysql_query($sql3) or die(mysql_error());
$row3 = mysql_fetch_assoc($res3) or die(mysql_error());
$oname = $row3['username'];

echo "<h2>".$alliancename."</h2><br />
<h3>Leader: ".$oname."</h3><br />
<p>News: ".$alliancenews."</p><br /><hr /><br />
<h2>Leave Alliance</h2><br />
<form action='php/leavealliance.php' method='post'><input type='submit' value='Leave Alliance' name='leave'></form>";

}
?>
</div>

 

PS: all session variables, etc. have been set on a login page.

 

mod edit: add code tags

Link to comment
Share on other sites

while ($row2 == mysql_fetch_assoc($res2) or die(mysql_error())) {

 

the double equals (==) and the or die(mysql_error() looks like your problem. changing it to the following should help.

 

while ($row2 = mysql_fetch_assoc($res2)) {

Link to comment
Share on other sites

Please put code in

 tags.

 

1. So, what is the output?

2. Is there an error perhaps? Do you have error reporting on? Are you using Apache?

 

Sorry, this was my first post. I'll put my code inside of code tags next time.

 

1. 

 

A. When the If ($aid == 0) statement is true, it prints out "Choose an Alliance," but doesn't list the top 5 in the database (the LIMIT 5 in the sql query).

B. When the else statement is true, the page is completely blank. Any ideas? Thanks in advance. Here's my code:

 

 

2. No errors print on the display. I am using apache, and how do I turn error reporting on? I get sql errors and errors (such as a missing ';' in my script).

 

 

-----------

 

 

while ($row2 == mysql_fetch_assoc($res2) or die(mysql_error())) {

 

the double equals (==) and the or die(mysql_error() looks like your problem. changing it to the following should help.

 

while ($row2 = mysql_fetch_assoc($res2)) {

 

I tried the php code, and I got the same output to my browser.

Link to comment
Share on other sites

Did you remove the or die() after all the mysql_fetch_assoc statements?

 

If there are no rows in a result set, the mysql_fetch_assoc statement returns a false value which causes the or die() statement to be executed. But because this isn't a mysql error, the mysql_error() output is empty and your page dies at that point with no output.

Link to comment
Share on other sites

If you run the query at command line or through phpMyAdmin what do you get?

It says Showing rows 0 - 0 (5 total, Query took 0.0005 sec) and shows me the alliances.

 

Did you remove the or die() after all the mysql_fetch_assoc statements?

 

If there are no rows in a result set, the mysql_fetch_assoc statement returns a false value which causes the or die() statement to be executed. But because this isn't a mysql error, the mysql_error() output is empty and your page dies at that point with no output.

 

It worked! Thanks, I have always used that in all of my php and I've never gotten any errors. Thank you for all of the help!

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.