Jump to content

[SOLVED] Only one row is showed


DEVILofDARKNESS

Recommended Posts

In the following code only one row is showen, although I know their are at least 2 rows that should be displayed.

 

this is the database:

region_id  nation_id  ammount

1--------------17----------5

2--------------18----------2

3--------------17----------2

 

(Like you can see If I run the code with nation_id 17 I should receive 2 outputs...)

 

$query = "SELECT * FROM regions WHERE nation_id='$nationid'";
			$result = mysql_query($query);
			$numrows = mysql_num_rows($result);
			if($numrows > 0){
				while($region = mysql_fetch_array($result)){
					$query2 = "SELECT * FROM region_weapons INNER JOIN weapons ON region_weapons.weapon_id = weapons.weapon_id WHERE region_id = '" . $region['region_id'] . "'";
					$result2 = mysql_query($query2);
					$YW = mysql_fetch_assoc($result2);
					echo "<div id='attack_left'>" . $YW['weapon_name'] . "</div><div id='attack_right'>" . $YW['ammount'] . "</div>";
				}
			}else{
				echo "<div id='attack_center'>This nation doesn't have any weapons...</div>";
			}

Link to comment
https://forums.phpfreaks.com/topic/170046-solved-only-one-row-is-showed/
Share on other sites

There is nothing wrong in the code above. Are you sure you have the data in db you say you have? Maybe you have two records in the main table but not any results for the another region in the weapons table? Since the only thing you are outputting is the results from weapons table.

Sorry the databases are this:

 

region_weapons

region_id  weapon_id  ammount

1-------------1-------------5

2-------------1-------------0

1-------------2-------------2

 

weapons

Attachment.

 

[attachment deleted by admin]

Oh yeas.. now I know it, inside the while loop you do this:

 

<?php
$YW = mysql_fetch_assoc($result2);

 

This gets only one row from $result2, and thats it. you should do another while loop inside the first while loop and loop there through the results in $result2. And also echo the results in $result2 also inside the second while loop.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.