Display Records from 'contacts' Database Table
Where the State is California, Ordering by Last Name

RECORDS
ID First Name Last Name Address City State Zip Phone
27 Ann Arms 4388 Folsom Street, Suite 673 Santa Clara CA 95050 (642) 915-9286
26 Richman Blue PO BOX 4971 Boulder CA 80302 (131) 664-3716
2 Lavaysse Circus 27 Woodland Avenue San Mateo CA 94402 (211) 107-2844
21 Anne Oaks 1941 aberdeen way Davis CA 95616 (143) 275-2569

There are 4 total records returned

 

CODE FOLLOWS

<?php

// set error reporting to all
error_reporting(E_ALL);
include(
'includes/header.php');

// declare connection variable that is populated by the open-db file
$con '';

?>
<h1>
    Display Records from 'contacts' Database Table
    <br> Where the State is California, Ordering by Last Name
</h1>
<!-- start of table -->
<table class="full">
    <tr>
        <th colspan=8 class="header1">
            RECORDS
        </th>
    </tr>
    <tr class="header2">
        <th>ID</th>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Address</th>
        <th>City</th>
        <th>State</th>
        <th>Zip</th>
        <th>Phone</th>
    </tr>

    <?php

    
// get records from the 'clients' table from the database
    // open db without exposing credentials
    
include('includes/open-db.php');

    
// SQL for selecting all records from the clients table where the state is California
    
$query "SELECT * FROM contacts WHERE state = 'CA' ORDER BY Last_Name ";
    
$result mysqli_query($con$query) or die("Could not get data: $query " mysqli_error());

    
// get and display records
    
$i 0;
    while (
$row mysqli_fetch_array($result))
    {
        
$id $row['id'];
        
$first_name $row['first_name'];
        
$last_name $row['last_name'];
        
$address $row['address'];
        
$city $row['city'];
        
$state $row['state'];
        
$zip $row['zip'];
        
$phone $row['phone'];
        
?>

        <tr class="row<?php echo($i++ & 1); ?>">
            <td>
                <?php echo($id); ?>
            </td>
            <td>
                <?php echo($first_name); ?>
            </td>
            <td>
                <?php echo($last_name); ?>
            </td>
            <td>
                <?php echo($address); ?>
            </td>
            <td>
                <?php echo($city); ?>
            </td>
            <td>
                <?php echo($state); ?>
            </td>
            <td>
                <?php echo($zip); ?>
            </td>
            <td>
                <?php echo($phone); ?>
            </td>
        </tr>

        <?php
        $total 
$row['total'];
    }

    
// close db
    
include('includes/close-db.php');
    
?>

</table>
<!-- end of table -->
<!--<hr>-->

<?php
echo("<p>There are $i total records returned</p>");

include(
'includes/footer.php');

?>