WHILE I tear my hair off!!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Rincewind
Forum Commoner
Posts: 27
Joined: Thu Nov 21, 2002 11:15 am
Location: Norway

WHILE I tear my hair off!!

Post by Rincewind »

I'm trying to make a message-board, and when listing replies below a message I'm trying to list them tab'ed in under the message it's a reply to.
I'm using this code:

Code: Select all

<html>
<head>
<title>Message</title>
</head>
<body>
<?php
include_once("php/pass.php");
$link = mysql_pconnect( $host, $user, $pass );
if( ! $link )
     die( "Couldn't connect to database-server!" );
mysql_select_db( $db, $link)
     or die("Couldn't connect to $db:".mysql_error() );
$id = $_GET&#1111;'id'];
$msg = mysql_query( "SELECT * FROM msg WHERE id = $id");
$head = mysql_result($msg, 0, head);
$body = mysql_result($msg, 0, body);
$parent = mysql_result($msg, 0, parent);
$grp = mysql_result($msg, 0, grp);
$id2 = $id;

function listchild($id)
&#123;
  global $id2;
  $msg = mysql_query( "SELECT head, child, id, parent FROM msg WHERE id = $id" );
  $child = mysql_result($msg, 0, child);
  $head = mysql_result($msg, 0, head);
  $parent = mysql_result($msg, 0, parent);
  $antparent = 0;
  while( $parent != 0 )
    &#123;
      $parent = mysql_query( "SELECT parent FROM msg WHERE id = $parent" );
      $parent = mysql_result($parent, 0, parent);
      ++$antparent;
    &#125;
  while( $antparent != 0 )
    &#123;
      print"<img src='blank.gif' width='20' height='20'>";
      --$antparent;
    &#125;
  if( $id != $id2 )
    &#123;
      echo"<a href=msg.php?id=$id>$head</a> --- $child<BR>";
    &#125;
  
  
  if( $child > 0)
    &#123;
      $msg = mysql_query( "SELECT head, child, id FROM msg WHERE parent = $id" );
      $childant = mysql_num_rows($msg);
      for ($nr = 0; $childant > $nr; ++$nr)
	&#123;
	  $idsend = mysql_result( $msg, $nr, id );
	  listchild($idsend); 
	&#125;
    &#125;
&#125;

echo"$head<BR>";
echo"$body<BR>";
echo"<a href=svar.php?parent=$id&?grp=$grp> Svar på dette innlegget </a><BR>";

listchild($id);

mysql_close($link);
?>
</body>
</html>
This works ok, except if I'm looking at a post which is a reply to a reply to a reply to a reply
the tab's just pushes the replies faaar to the right on my screen, even the first one, because the first one has a lot of parents even if they are not shown...so I tried this in stead:

Code: Select all

<html>
<head>
<title>Message</title>
</head>
<body>
<?php
include_once("php/pass.php");
$link = mysql_pconnect( $host, $user, $pass );
if( ! $link )
     die( "Couldn't connect to database-server!" );
mysql_select_db( $db, $link)
     or die("Couldn't connect to $db:".mysql_error() );
$id = $_GET&#1111;'id'];
$msg = mysql_query( "SELECT * FROM msg WHERE id = $id");
$head = mysql_result($msg, 0, head);
$body = mysql_result($msg, 0, body);
$parent = mysql_result($msg, 0, parent);
$grp = mysql_result($msg, 0, grp);
$id2 = $id;

function listchild($id)
&#123;
  global $id2;
  $msg = mysql_query( "SELECT head, child, id, parent FROM msg WHERE id = $id" );
  $child = mysql_result($msg, 0, child);
  $head = mysql_result($msg, 0, head);
  $parent = mysql_result($msg, 0, parent);
  $antparent = 0;
  while( $parent != $id )
    &#123;
      $parent = mysql_query( "SELECT parent FROM msg WHERE id = $parent" );
      $parent = mysql_result($parent, 0, parent);
      ++$antparent;
    &#125;
  while( $antparent != 0 )
    &#123;
      print"<img src='blank.gif' width='20' height='20'>";
      --$antparent;
    &#125;
  if( $id != $id2 )
    &#123;
      echo"<a href=msg.php?id=$id>$head</a> --- $child<BR>";
    &#125;
  
  
  if( $child > 0)
    &#123;
      $msg = mysql_query( "SELECT head, child, id FROM msg WHERE parent = $id" );
      $childant = mysql_num_rows($msg);
      for ($nr = 0; $childant > $nr; ++$nr)
	&#123;
	  $idsend = mysql_result( $msg, $nr, id );
	  listchild($idsend); 
	&#125;
    &#125;
&#125;

echo"$head<BR>";
echo"$body<BR>";
echo"<a href=svar.php?parent=$id&?grp=$grp> Svar på dette innlegget </a><BR>";

listchild($id);

mysql_close($link);
?>
</body>
</html>
Because I figured then it would stop counting parents at the current message, but here the trouble begins:
When I try to run this code I get:

Header of current message.
Body of current message.
Warning: Unable to jump to row 0 on MySQL result index 4 in /www/htdocs/msg-board/msg.php on line 32

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /www/htdocs/msg-board/msg.php on line 32

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /www/htdocs/msg-board/msg.php on line 32

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /www/htdocs/msg-board/msg.php on line 32

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /www/htdocs/msg-board/msg.php on line 32

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /www/htdocs/msg-board/msg.php on line 32

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /www/htdocs/msg-board/msg.php on line 32

This goes on forever....

Any hints anyone?
I'm probably just tired and stupid, but I don't get it, anyone?

Thanx.

Rincewind_the_Wizzard
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You probably need to add some error handling to your mysql_query() calls to find out why the result is not valid. Try changing things like this:

Code: Select all

$msg = mysql_query( "SELECT * FROM msg WHERE id = $id");
to

Code: Select all

$sql = "SELECT * FROM msg WHERE id = $id";
$msg = mysql_query($sql) or die(mysql_error().'<p>'.$sql.'</p>');
Mac
Rincewind
Forum Commoner
Posts: 27
Joined: Thu Nov 21, 2002 11:15 am
Location: Norway

Post by Rincewind »

Doesn't do me much good really, first of all I just get

Code: Select all

You have an error in your sql-syntax near "  in line 1
Which doesn't tell me overly much, at least not since the same query works fine when the while statement is While( $parent != 0 ) but fails when the statement is While( $parent != $id )
That is what I don't get, why is the operands of the while-statement affecting the mysql_query?

Any ideas?

Thanx

Rincewind
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Have you echoed out the SQL statement to make sure it looks as expected in case a variable isn't getting passed properly?

Mac
Rincewind
Forum Commoner
Posts: 27
Joined: Thu Nov 21, 2002 11:15 am
Location: Norway

Post by Rincewind »

Got it to work now :)
Thanx for the help twigletmac!

Rincewind
Post Reply