DEPARTMENT OF COMPUTER ENGINEERING
Subject: Mobile Application Development Subject Code: 22619
Semester: 6 Course: CO6I-A
Laboratory No: Name of Subject Teacher:
Name of Student: AFIF IRFAN NAZIR Roll Id: 21203A1005
Experiment No: 15
Title of Experiment Develop a simple application to 1. Enter data into database 2. Retrieve
and present data from database.
Practical Related Question
1. Which command would you use to view the available databases or tables?
In PHP, to view the available databases, you would use the SHOW DATABASES;
command. To view the available tables in a database, you would first need to select the
database using the USE command, and then use the SHOW TABLES;
2. How can you view the structure of a table?
To view the structure of a table in PHP, you can use the "DESCRIBE" or "SHOW
COLUMNS" statement. These statements are used to display the column names, data
types, and constraints of a table. Here's an example of how to use the "DESCRIBE"
statement:
3. How is it possible to know the number of rows returned in the result set?
To know the number of rows returned in the result set, we can use the mysqli_num_rows()
function in PHP. This function takes the result set returned by a SELECT query as its
parameter and returns the number of rows in the result set.
4. Which function returns the number of affected entries by a query?
The mysqli_affected_rows() function returns the number of affected rows in the previous
operation performed on the database through the specified MySQLi connection. For
example, if a query inserts or updates 5 rows, this function will return 5.
Exercise Related Question
1. Write a PHP script to enter data in database
<?php
// Set database connection parameters
$servername = "localhost";
$username = "yourusername";
$password = "yourpassword";
$dbname = "yourdbname";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Retrieve data from form
$name = $_POST["name"];
$email = $_POST["email"];
$phone = $_POST["phone"];
// Prepare SQL statement
$stmt = $conn->prepare("INSERT INTO customers (name, email,
phone) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $name, $email, $phone);
// Execute SQL statement
if ($stmt->execute() === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $stmt->error;
}
// Close statement and connection
$stmt->close();
$conn->close();
?>
2. Write a PHP script to retrieve and present the data from database.
<?php
// Step 1: Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
$conn = mysqli_connect($servername, $username, $password,
$dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Step 2: Execute the SELECT query
$sql = "SELECT id, firstname, lastname, email FROM MyGuests";
$result = mysqli_query($conn, $sql);
// Step 3: Loop through the rows and display the data
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " .
$row["lastname"]. " - Email: " . $row["email"]. "<br>";
}
} else {
echo "0 results";
}
// Step 4: Close the database connection
mysqli_close($conn);
?>