Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //Set up the database access credentials
- $hostname = 'localhost';
- $username = 'root';
- $password = '';
- $databaseName = 'C3459845';
- //Open the database connection - exit with error message otherwise
- $connection = mysqli_connect($hostname, $username, $password, $databaseName) or exit("Unable to connect to database!");
- ?>
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- <?php
- //Make connection to database
- include 'connection.php';
- //(a)Gather from $_POST[]all the data submitted and store in variables
- if(isset($_POST['custdetailsSubmit'])){
- $firstName = $_POST['txtfname'];
- $surName = $_POST['txtsname'];
- $email = $_POST['txtemail'];
- $passwd = $_POST['passtxt'];
- $gender = $_POST['gen'];
- $age = $_POST['age'];
- }
- else{
- //DO NOTHING
- }
- //(b)Construct INSERT query using variables holding data gathered
- $query = "INSERT INTO Customer (FirstName, LastName, Email,
- Password, Gender, Age) VALUES ('$firstName', '$surName', '$email', '$passwd', '$gender', '$age')";
- mysqli_query($connection, $query);
- if ($connection->query($sql) === TRUE) {
- echo "New record created successfully";
- } else {
- echo "Error: " . $sql . "<br>" . $conn->error;
- }
- $conn->close();
- ?>
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- <?php
- //Make connection to database
- include 'connection.php';
- //Display heading
- echo '<h2>Select ALL from the Customer Table</h2>';
- //run query to select all records from customer table
- $query="SELECT * FROM Customer";
- //store the result of the query in a variable called $result
- $result=mysqli_query($connection, $query);
- //Use a while loop to iterate through your $result array and display
- //the first name, last name and email for each record
- while ($row=mysqli_fetch_assoc($result)){
- echo $row['FirstName'].' '.$row['LastName'].' '.$row['Email'].'<br />';
- }
- echo '<h2>Select ALL from the Customer Table with Age > 22</h2>';
- $query = "SELECT * FROM Customer WHERE Age > 22";
- $result = mysqli_query($connection, $query);
- while ($row=mysqli_fetch_assoc($result)){
- echo $row['FirstName'].' '.$row['LastName'].' '.$row['Email'].' '.$row['Age']. '<br />';
- }
- echo '<h2>Select ALL from the Customer Table with Age >= 22</h2>';
- $query = "SELECT * FROM Customer WHERE Age >= 22 AND Gender = 'M'";
- $result = mysqli_query($connection, $query);
- while ($row=mysqli_fetch_assoc($result)){
- echo $row['FirstName'].' '.$row['LastName'].' '.$row['Email'].' '.$row['Age']. '<br />';
- }
- echo '<h2>Select Males from the Customer Table - List by Age descending</h2>';
- $query = "SELECT * FROM Customer WHERE Gender = 'M' GROUP BY Age DESC";
- $result = mysqli_query($connection, $query);
- while ($row=mysqli_fetch_assoc($result)){
- echo $row['FirstName'].' '.$row['LastName'].' '.$row['Email'].' '.$row['Age']. '<br />';
- }
- echo '<h2>Select ALL from the Customer Table with "a" in First Name > 22</h2>';
- $query = "SELECT * FROM Customer WHERE FirstName LIKE '%a%'";
- $result = mysqli_query($connection, $query);
- while ($row=mysqli_fetch_assoc($result)){
- echo $row['FirstName'].' '.$row['LastName'].' '.$row['Email'].' '.$row['Age']. '<br />';
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement