Script 10.1 View Users (basic)

Output
Source
<?php # Script  10.1 BASIC view users with Count
// This script retrieves all the records from the users table.
 
include (CHAPTER_PATH.'/'.$chapter.'/includes/header.php');

echo "<div id='content'>";
echo '<h1>Registered Users</h1>';

// connect to the db
require(CONNECT);

// build query 
$q = "SELECT last_name, first_name, email, DATE_FORMAT(registration_date, '%M %d, %Y') AS dr, user_id FROM site_users ORDER BY last_name ASC";

// run query
if($r = @mysqli_query ($link, $q)) { 
	// Count the number of returned rows:
	$num = mysqli_num_rows($r);

	if($num > 0) {
		// Print how many users there are:
		echo "<div class='message'>There are currently $num registered users.</div>\n";
		
		// Table header:
		echo '<table>
		<thead>
		<tr>
			<th><b>Edit</b></th>
			<th><b>Delete</b></th>
			<th><b>Last Name</b></th>
			<th><b>First Name</b></th>
			<th><b>Email</b></th>
			<th><b>Date Registered</b></th>
		</tr></thead><tbody>';
	
		// Fetch and print all the records:
		while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
			echo '<tr>
				<td><a href="index.php?chapter=10&amp;script=10.3&amp;id=' . $row['user_id'] . '">Edit</a></td>
				<td><a href="index.php?chapter=10&amp;script=10.2&amp;id=' . $row['user_id'] . '">Delete</a></td>
				<td>' . $row['last_name'] . '</td>
				<td>' . $row['first_name'] . '</td>
				<td>' . $row['email'] . '</td>
				<td>' . $row['dr'] . '</td>
			</tr>';
		}
		echo '<tbody></table>';
		mysqli_free_result ($r);
	} else {
		// no results
		echo '<p class="error-message error">There are currently no registered users.</p>';
	}

} else {
	//query unsuccessful
	echo '<h2>Error</h2><p class="error-message error">There was an error accessing the database. Please try again later.</p>';
}
// close db connection 
require(DISCONNECT);

echo "</div>";
include (CHAPTER_PATH.'/'.$chapter.'/includes/footer.php');
?>