Re: Query refuses to recurse all rows

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



celtic@xxxxxxxxxx wrote:
Hi,
In the following code, only the first row from query1 displays along with the display stuff from query3. The problem: query1 refuses to recurse to the next and following rows. It appears that the sub-queries in the main query cause the $row++; not to work.

Is there some conditional that might it prompt it to do so? I'm stumped - worked at variations for a few days. Any help, pointers, suggestions most gratefully welcome!



<?php

include("dbc.php");
if (!$db){ $_SESSION['db'] = "select-thread"; session_write_close(); header("location: db.php");}

	$query1 = "SELECT * FROM replies WHERE mid = '{$_SESSION['mid']}'";
							
		$result1 = pg_exec($db, $query1);
		if (!$result1) { exit;}
	
		$numrows1 = pg_numrows($result1);
		$row = 0;
		do
		{
			$myrow = pg_fetch_array($result1, $row);	
	
				/* display stuff */



$query2 = "SELECT * FROM subs WHERE mid = '{$_SESSION['mid']}' AND rid = '{$myrow['rid']}'";

					$result2 = pg_exec($db, $query2);
					if (!$result2) { exit;}
				
					$numrows2 = pg_numrows($result2);
					$row = 0;
					do
					{
						$myrow = pg_fetch_array($result2, $row);	
		
							/* display stuff */
	
					$row++;
					}
					while ($row < $numrows2);
	
	
	
	
$query3 = "SELECT * FROM subs where subsid = '{$_SESSION['subsid']}' AND rid = '{$myrow['rid']}' AND responded = '1'";
	
					$result3 = pg_exec($db, $query3);
					if (!$result3) { exit;}
				
					$numrows3 = pg_numrows($result3);
					$row = 0;
					do
					{
					$myrow = pg_fetch_array($result3, $row);	
	
						/* display stuff */
	
					$row++;
					}
					while ($row < $numrows3);....

		
		$row++;		
		}
		while ($row < $numrows1);

?>



If I may, I would like to suggest a few other changes that you might like.

first off, don't use counters, they waist cpu cycles.  Try this code out.


<?php

include("dbc.php");
if (!$db){ $_SESSION['db'] = "select-thread"; session_write_close();

// Whhhhaaaattttt........???  what is this for?
header("location: db.php");}
#
#
#
# why are you displaying anything after a header('Location ....') call?


// Build first query
$query1 = "SELECT *
           FROM   replies
           WHERE  mid = '{$_SESSION['mid']}'";
							
// Execute query. if it fails, skip this block
if ( ($result1 = pg_exec($db, $query1)) !== false ) {

  // if we are here, the query worked.  Loop through results
  while ( $row1 = pg_fetch_assoc($result1) ) {

    /* display stuff */

    // Build second query
    $query2 = "SELECT *
               FROM   subs
               WHERE  mid = '{$_SESSION['mid']}'
               AND    rid = '{$row1['rid']}'";


    // Execute second query, if it fails, skip this block
    if ( ($result2 = pg_exec($db, $query2)) !== false ) {
      // If we are here, the query worked.  Loop through results
      while ( $row2 = pg_fetch_assoc($result2) ) {
        /* display stuff */
      }
    }

    // Build third query
    $query3 = "SELECT *
               FROM   subs
               WHERE  subsid = '{$_SESSION['subsid']}'
               AND    rid = '{$row1['rid']}'
               AND    responded = '1'";

    // Execute third query, if it fails, skip this block
    if ( ($result3 = pg_exec($db, $query3)) !== false ) {
      // If we are here, the query worked.  Loop through results
      while ( $row3 = pg_fetch_assoc($result3) ) {
        /* display stuff */
      }
    }
  }
}

?>

My guess is is that you don't want to actually 'exit;' the script if a query fails, you just wanted to skip displaying any data for it. I might be wrong, but I adjusted my example to factor this in.

Inquiring minds want to know. Why is it that you have a header() call with a location header that tells this script to go somewhere else, but then you display a bunch of data after the header call is sent to the client?


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux