Jump to content

[SOLVED] Error with while function.


Anzeo

Recommended Posts

hi everyone, decided to start a new topic concerning this matter (however it's related to my previous post).

 

Anyway, I've just started working on my own forum. I already have the table structure and in the first steps I want only a couple of forums working (like the news section and such). But next to that I've already started on my forum index to show all categories and their respective forums.

 

Now the basic code I've written for this is:

 

<table width="700" border="0" cellspacing="0" cellpadding="0">
<?php
/* these two lines are my connection set up, so I left them out,
*/
while($catqry = mysql_fetch_array(mysql_query("SELECT * FROM CATEGORIE ORDER BY VolgNr")))
{
	$CatNaam = $catqry["Naam"];
	$CatId = $catqry["ID"];
?>
<tr><td><a href="view.php?cat=<?php echo "$CatId";?>"><?php echo "$CatNaam"; ?></a></td></tr>
<?php
	$frmrowsqry = mysql_fetch_array(mysql_query("SELECT COUNT(*) AS Limit FROM FORUM WHERE Categorie='$CatId'"));
	$Limit = $frmrowsqry["Limit"];
	while($frmqry = mysql_fetch_array(mysql_query("SELECT * FROM FORUM WHERE Categorie = '$CatId' ORDER BY VolgNr LIMIT='$Limit'")))
	{
		$FrmNaam = $frmqry["Naam"];
		$FrmId = $frmqry["ID"];
		$FrmBeschrijving = $frmqry["Beschrijving"];
?>
<tr><td><a href="vieuw.php?frm=<?php echo "$FrmId";?>"><?php echo "$FrmNaam";?></a></td></tr>
<?php
	}
}
?>
</table>

 

and when I run this on my site I get this output

 

NEWS

General

General

General

General

...

..

.

 

So instead of fetching the forum only once it loops indefinatly. Anyone knows what in my code is causing it to loop like this?

 

Any help greatly appreciated,

Thanks In Advance

Link to comment
https://forums.phpfreaks.com/topic/46564-solved-error-with-while-function/
Share on other sites

while($catqry = mysql_fetch_array(mysql_query("SELECT * FROM CATEGORIE ORDER BY VolgNr")))

 

you get a new resource everytime... thats why it loops forever.

you should do like this

 

$sql   = " SELECT * from ...";
$res   = mysql_query($sql) or die(mysql_error());
while($rows = mysql_fetch_array($res))
{
  ....
...

Can't seem to get this working, this is the code I'm using to test wether it fetches the forums and categories, but I get no output

 

<table width="700" border="0" cellspacing="0" cellpadding="0">
<?php
include("connect.php");
while($catqry = mysql_fetch_array(mysql_query("SELECT * FROM FORUM INNER JOIN CATEGORIE ON FORUM.Categorie = CATEGORIE.ID ORDER BY CATEGORIE.VolgNr")))
{
	$CatNaam = $catqry["CATEGORIE.Naam"];
	$CatId = $catqry["CATEGORIE.ID"];
	$FrmNaam = $frmqry["FORUM.Naam"];
	$FrmId = $frmqry["FORUM.ID"];
	$FrmBeschrijving = $frmqry["FORUM.Beschrijving"];
?>
<tr><td><a href="vieuw.php?mode=frm&fid=<?php echo "$FrmId";?>"><?php echo "$FrmNaam";?></a></td></tr>
<?php
}
?>
</table>

 

Can someone point me out where I'm wrong?

Thanks in advance

<table width="700" border="0" cellspacing="0" cellpadding="0">
<?php
include("connect.php");
$qry = "SELECT FORUM.Naam FROM FORUM INNER JOIN CATEGORIE ON FORUM.Categorie = CATEGORIE.ID ORDER BY CATEGORIE.VolgNr";
if($result = mysql_query($qry))
{
	if(mysql_num_rows($result))
	{
		while($catqry = mysql_fetch_array($result))
		{
			$CatNaam = $catqry["CATEGORIE.Naam"];
			$CatId = $catqry["CATEGORIE.ID"];
			$FrmNaam = $catqry["FORUM.Naam"];
			$FrmId = $catqry["FORUM.ID"];
			$FrmBeschrijving = $catqry["FORUM.Beschrijving"];
?>
<tr><td><a href="vieuw.php?mode=frm&fid=<?php echo "$FrmId";?>"><?php echo "$FrmNaam";?></a></td></tr>
<?php
		} 
	}
	else
	{
		echo "No results found";
	}
}
else 
{
	echo "Query failed <br />$sql<br />" . mysql_error();
}
?>
</table>

 

I realised the code in my previous post was wrong so I altered it to this code, which I believe is right, but I still get no output.. Anyone who knows what I'm doing wrong here?

 

Thanks in advance

Testing Output
<table width="700" border="0" cellspacing="0" cellpadding="0">
<?php
include("connect.php");
$qry = "SELECT FORUM.Naam FROM FORUM INNER JOIN CATEGORIE ON FORUM.Categorie = CATEGORIE.ID ORDER BY CATEGORIE.VolgNr";
        print "<tr><td>" . $qry . "</td></tr>";
if($result = mysql_query($qry))
{
	if(mysql_num_rows($result))
	{
		while($catqry = mysql_fetch_array($result))
		{
			$CatNaam = $catqry["CATEGORIE.Naam"];
			$CatId = $catqry["CATEGORIE.ID"];
			$FrmNaam = $catqry["FORUM.Naam"];
			$FrmId = $catqry["FORUM.ID"];
			$FrmBeschrijving = $catqry["FORUM.Beschrijving"];
?>
<tr><td><a href="vieuw.php?mode=frm&fid=<?php echo "$FrmId";?>"><?php echo "$FrmNaam";?></a></td></tr>
<?php
		} 
	}
	else
	{
		echo "No results found";
	}
}
else 
{
	echo "Query failed <br />$sql<br />" . mysql_error();
}
?>
</table>

 

Try running that see if you still get a blank page.

Testing Output
<table width="700" border="0" cellspacing="0" cellpadding="0">
<?php
include("connect.php");
$qry = "SELECT FORUM.Naam FROM FORUM INNER JOIN CATEGORIE ON FORUM.Categorie = CATEGORIE.ID ORDER BY CATEGORIE.VolgNr";
        print "<tr><td>";
if($result = mysql_query($qry))
{
	if(mysql_num_rows($result) > 0)
	{
		while($catqry = mysql_fetch_array($result))
		{
			$CatNaam = $catqry["CATEGORIE.Naam"];
			$CatId = $catqry["CATEGORIE.ID"];
			$FrmNaam = $catqry["FORUM.Naam"];
			$FrmId = $catqry["FORUM.ID"];
			$FrmBeschrijving = $catqry["FORUM.Beschrijving"];
?>
<tr><td><a href="vieuw.php?mode=frm&fid=<?php echo "$FrmId";?>"><?php echo "$FrmNaam";?></a></td></tr>
<?php
		} 
	}
	else
	{
		echo "No results found";
	}
}
else 
{
	echo "Query failed <br />$sql<br />" . mysql_error();
}
       print "</td></tr>";
?>
</table>

 

See what happens there.

You might want to rename that column.  It's smarter to use an identifier if you know two columns will be joined together.  I usually use the first letter of the table name.  For example, if I have a table named categories, I'd prefix all of the column names with c_, such as:

 

c_id, c_name

 

That way, when you join two tables together, you never have two of the same column names.

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.