Page 1 of 1

Unknown PHP/SQL Problem

Posted: Fri Oct 22, 2004 6:41 pm
by grahamd
Hey peeps! I'm having a problem here which is really starting to rip my socks. For my uni project I'm undertaking a PHP/MySQL powered user login/forum website design. Things were running smoothly till the other day....

I have an admin section for the logon system with a page called "view_users.php". View Users reads in all the registered users from the database and bungs their info into a table on the page. I took this code from my trusty PHP book incidentally. The table was working fine and dandy until I had over 10 users registered. Once 11 records or more exist the code automatically breaks the table into pages to facilitate reading. This is where my problem lies - the pages break up properly with a back and next link - but the table disappears!

One bizarre glitch I noticed relating to it is that the CSS takes a few seconds to load sometimes on the pages - the table appears to display for these few seconds before disappearing! Baffling. It's killing me! Any help would be vastly appreciated! Cheers guys! Here's the whole page code:

Code: Select all

<?php # view_users.php
// This page allows the administrator to view all of the current users.

// Include the configuration file for error management and such.
require_once ('../includes/config.inc'); 

// Require authentication.
require_once ('../../authentication.php');

// Set the page title and include the HTML header.
$page_title = 'View the Current Users';
include_once ('../includes/admin_header.html');

// Check for authorization.
if (!$authorized) {
	echo '<p><font color="red">Please enter a valid username and password! Click <a href="index.php">here</a> to try again!</font></p>';
} else {

	require_once ('../../mysql_connect.php'); // Connect to the database.

	// Number of records to show per page:
	$display = 10;
	
	// Determine how many pages there are. 
	if (isset($_GET['np'])) { // Already been determined.
		$num_pages = $_GET['np'];
	} else { // Need to determine.
		$query = "SELECT CONCAT(last_name, ', ', first_name) AS name, DATE_FORMAT(registration_date, '%M %d, %Y') AS dr FROM users ORDER BY registration_date ASC"; // Standard query.
		$query_result = mysql_query ($query);
		$num_records = @mysql_num_rows ($query_result);
		
		if ($num_records > $display) { // More than 1 page.
			$num_pages = ceil ($num_records/$display);
		} else {
			$num_pages = 1;
		}
	}
	
	// Determine where in the database to start returning results.
	if (isset($_GET['s'])) { // Already been determined.
		$start = $_GET['s'];
	} else {
		$start = 0;
	}
			
	// Make the query.
	$query = "SELECT user_id AS id, CONCAT(last_name, ', ', first_name) AS name, DATE_FORMAT(registration_date, '%M %d, %Y') AS dr, email AS em, location AS loc FROM users ORDER BY registration_date ASC LIMIT $start, $display";		
	$result = @mysql_query ($query); // Run the query.
	$num = mysql_num_rows ($result); // How many users are there?
	
	if ($num > 0) { // If it ran OK, display the records.
	
		echo "<h3>Registered Users</h3>";
	
		// Make the links to other pages, if necessary.
		if ($num_pages > 1) {
			
			echo '<p>';
			// Determine what page the script is on.	
			$current_page = ($start/$display) + 1;
			
			// If it's not the first page, make a Previous button.
			if ($current_page != 1) {
				echo '<a href="view_users.php?s=' . ($start - $display) . '&np=' . $num_pages . '">Previous</a> ';
			}
			
			// Make all the numbered pages.
			for ($i = 1; $i <= $num_pages; $i++) {
				if ($i != $current_page) {
					echo '<a href="view_users.php?s=' . (($display * ($i - 1))) . '&np=' . $num_pages . '">' . $i . '</a> ';
				} else {
					echo $i . ' ';
				}
			}
			
			// If it's not the last page, make a Next button.
			if ($current_page != $num_pages) {
				echo '<a href="view_users.php?s=' . ($start + $display) . '&np=' . $num_pages . '">Next</a>';
			}
			
			echo '</p><br />';
			
		} // End of links section.
		
		// Table header.
		echo '<table align="left" cellspacing="1" cellpadding="2">
		<tr><td align="left"><div class="p2"><b>ID&nbsp;</b></div></td><td align="left"><div class="p2"><b>Name&nbsp;</b></div></td><td align="left"><div class="p2"><b>Registered</b></div></td> <td align="left"><div class="p2"><b>Email</b></div></td> <td align="left"><div class="p2"><b>Location</b></div></td></tr>';
		
		// Fetch and print all the records.
		$bg = '#eeeeee'; // Set the background color.
		while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
   			$bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee'); // Switch the background color.
			echo '<tr bgcolor="', $bg, '"><td align="left"><div class="p2">', $row[0], '&nbsp;&nbsp;&nbsp;</div></td><td align="left"><div class="p2">', stripslashes($row[1]), '&nbsp;&nbsp;&nbsp;</div></td><td align="left"><div class="p2">', $row[2], '&nbsp;&nbsp;&nbsp;</div></td><td align="left"><div class="p2"><a href=mailto:', $row[3], '>', $row[3], '</a>&nbsp;&nbsp;&nbsp;</div></td><td align="left"><div class="p2">', $row[4], '&nbsp;&nbsp;&nbsp;</div></td></tr>';
		}
	
		echo '</table>'; // Close the table.

		mysql_free_result ($result); // Free up the resources.	
	
	} else { // If there are no registered users.
		echo '<h5>There are currently no registered users.</h5>'; 
	}
	
	mysql_close(); // Close the database connection.
}

include_once ('../includes/admin_footer.html'); // Use the HTML footer file.
?>

Posted: Fri Oct 22, 2004 10:26 pm
by kettle_drum
Well what is being shown on the page? View the source to see how far the script runs too, and what it misses out. My guess is the query will be incorrect for the pages greater than 1 and so it doesnt run the loop to print out the data as there is nothing to loop through. Echo out the queryr string to see what it looks like to see if you have a valid query.

Internet Explorer Strikes Again

Posted: Sat Oct 23, 2004 7:24 am
by grahamd
Right - one minor discovery in relation to my problem which might throw some light on the problem - it's an Internet Explorer only problem. The page displays fine in Netscape. Why on earth could this be happening?