Jump to content

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))
{
  ....
...

Its would be better if you used joins for your query.

 

That way you will have 1 query and 1 while loop.

 

Overtime if you continue with that code your will cause stress on on the MySQL server. Look in to SQL Optimization.

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.

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.