Pull All Records from a Database Table (i.e., clients table)

RECORDS
ID Name Address 1 Address 2 City State Zip Phone
1 Gorman Place 8 Horizon Road New Haven CT 06510 (336) 902-1538
2 Lavaysse Circus 27 Woodland Avenue San Mateo CA 94402 (211) 107-2844
3 Weisman Oil 3 Cindy Court Englewood NJ 07631 (276) 673-3707
4 Garrett Mag 83 Gentry Drive Melville NY 11747 (521) 333-7762
5 Carie Max Stores 236 Stanton Street Brookline MA 02446 (180) 351-8977
6 Mason Homes 587 Nathan Hale Road Bonita Springs FL 34134 (638) 895-7446
7 Bank of New York 2461 Riverside Dr. Apt. 9079 New York NY 10002 (123) 720-9779
8 Weitzman Construction 442 W. 98th St # 886 New York NY 10009 (736) 854-3262
9 Lambert Homes 841 central park west Apt. 774 St Davids PA 19087 (139) 523-8779
10 Rubin Sands 134 Iven Avenue Suite 2E New York NY 10024 (386) 634-8851
11 Rags to Riches Inc. 9739 85th Street New York NY 10003 (571) 811-5584
12 Jett Lee Sales 918 Central Ave. #313 Christiansted VI 00823 (841) 332-7923
13 Mcadams Investments Box 146 New York City NY 10003 (515) 650-2411
14 HallMark Rentals Livio Andronico 22 New York NY 10025 (534) 289-1364
15 Parson Computers 300 Knoll Blvd. Wayne NJ 07470 (279) 258-2678
16 Jenkinson Loan 8 Sundance Drive Main Road Mason MI 48920 (825) 281-8880
17 Minnick Lands 80 Hudson Street Suite 159 Bellevue WA 98004 (803) 918-7430
18 Scheiby Loans 5219 433th Ave SE Hastings-on-Hudson NY 10706 (129) 957-2662
19 Calvin Klien 32 Gray Gardens East Broadlands VA 20148 (793) 815-6116
20 Walker Floors 794 A Fairmont Ave. Hoboken NJ 07030 (351) 656-8438
21 Sherwood Oaks 1941 aberdeen way Davis CA 95616 (143) 275-2569
22 Coburn Homes 92525 N Meridian Pleasantville NY 10570 (365) 404-2548
23 Higgins Lakefront P.O. Box 664 Suite 384 Indianapolis IN 46290 (533) 230-4366
24 Davis Lodge 492 Locust Road Charlotte NC 28204 (164) 963-3123
25 Easter Travel 4114 W. Elm Suite 370 Stratham CA 03885 (330) 871-5564
26 Richman Buick PO BOX 4971 Boulder CA 80302 (131) 664-3716
27 Daly Autobody 4388 Folsom Street Suite 673 Santa Clara CA 95050 (642) 915-9286
28 Weston Electric 100 44th Ave.SE Suite 913A Glenview IL 60026 (486) 959-2036
29 Daves Diner 4585 W. Lake Norman OK 73026 (643) 147-7945
30 Eastman Rentals 600 Art Blvd. Lansing MI 48911 (158) 201-5442
31 Malcom Motors 5432 Sunset #A12 Charlotte SC 65432 (987) 543-8765

 

CODE FOLLOWS

<?php

// Enable error reporting for all types of errors
error_reporting(E_ALL);

// Include the header file for page setup
include('includes/header.php');

// Initialize an empty connection variable (though it should be set later)
$con '';

?>
<h1>Pull All Records from a Database Table (i.e., clients table)</h1>

<!-- Start of table to display client records -->
<table class="full">
    <tr>
        <!-- Table header with a colspan of 8 for styling purposes -->
        <th colspan="8" class="header1">RECORDS</th>
    </tr>
    <tr class="header2">
        <!-- Column headers -->
        <th>ID</th>
        <th>Name</th>
        <th>Address 1</th>
        <th>Address 2</th>
        <th>City</th>
        <th>State</th>
        <th>Zip</th>
        <th>Phone</th>
    </tr>

    <?php
    
// Include the database connection file
    
include('includes/open_db.php');

    
// Define the SQL query to retrieve all records from the clients table where id > 0
    
$query "    SELECT * FROM clients WHERE id > '0' ";

    
// Execute the query; handle errors if the query fails
    
$result mysqli_query($con$query) or die("Could not get data: $query " mysqli_error($con));

    
// Loop through each row in the result set and display it in the table
    
$i 0;
    while (
$row mysqli_fetch_array($result)) {
        
// Extract data from each row into individual variables
        
$id $row['id'];
        
$name $row['name'];
        
$address_1 $row['address_1'];
        
$address_2 $row['address_2'];
        
$city $row['city'];
        
$state $row['state'];
        
$zip $row['zip'];
        
$phone $row['phone'];
        
?>

        <!-- Display each row's data in a table row, with alternating classes for styling -->
        <tr class="row<?php echo($i++ & 1); ?>">
            <td><?php echo htmlspecialchars($id); ?></td>
            <td><?php echo htmlspecialchars($name); ?></td>
            <td><?php echo htmlspecialchars($address_1); ?></td>
            <td><?php echo htmlspecialchars($address_2); ?></td>
            <td><?php echo htmlspecialchars($city); ?></td>
            <td><?php echo htmlspecialchars($state); ?></td>
            <td><?php echo htmlspecialchars($zip); ?></td>
            <td><?php echo htmlspecialchars($phone); ?></td>
        </tr>

        <?php
    
}

    
// Close the database connection
    
include('includes/close_db.php');
    
?>

</table>

<hr>

<?php
// Include the footer file to close the HTML structure
include('includes/footer.php');
?>