Jump to content

foreach loop on query


Sharkadder

Recommended Posts

Hi there,I have a foreach loop which loops through items in an array in PHP. Each value in the array is then checked and a column in a database table is checked. If the item is in the database i wish to print out the results onto the screen.The problem i have is that it seems to check the first item from the array and then it doesn't check the rest. I have looked on the internet about searching items in a foreach loop and doing a mysql query for each item and it seems to be a common problem.My code is as follows:

$clubs = array(Liverpool[eng](1996-2004), Real Madrid(2004-2005), Newcastle United(2005-2009), Manchester United(2009-Present));	#loop through items in array $clubs	foreach ($clubs as $club)	{		#split the club up by "(" to just leave the team name i.e. removing the years		$clubname = explode("(", $club);		#search through database where the teamname is equal to $clubname		$query1 = "SELECT * FROM ClubID WHERE teamname = '$clubname[0]'";		$result1=mysql_query($query1) or exit(mysql_error());		$num1=mysql_numrows($result1);		$i=0;		while ($i < $num1)		{			#since the teamname might have been found more than once we do while loop here			#print out the result and then increment inner loop			print mysql_result($result1,$i,"teamname");			$i++;		}	}

ok so that is my code, it prints out the first team name ok, i.e. Liverpool[eng] prints out fine but the rest do not, any ideas why it will not keep looping through the rest of the clubs inside the array $clubs? All of the teams do exist in the database and the spellings are all correct.Many thanks,Mark

Link to comment
Share on other sites

The only reason I can tell is if it's not found in the database. You can verify that by printing out something like the number of rows that were returned, or print out the SQL query so you can run it in something like phpMyAdmin.

Link to comment
Share on other sites

ok i think i sorted it...when i printed the rows out it was saying 0 entries found for the last 3 clubs, i then trimmed the line and they must have been a space before the name which is why it found nothing. The space obviously came after looping through the array. Anyways it has worked now so thanks for that. I spent a whole 3 hours stareing wondering where i went wrong too...never to mind.Thanks for the help dude, the problem is now solved.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...