0% found this document useful (0 votes)
26 views2 pages

PHP Practical No.15 Outputs

The document contains PHP code examples for database operations. The first example demonstrates how to retrieve and display user data from a database, while the second example shows how to insert a new employee record into an employee table. Both examples include steps for establishing a database connection and handling errors.

Uploaded by

ARYAN MOHADE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views2 pages

PHP Practical No.15 Outputs

The document contains PHP code examples for database operations. The first example demonstrates how to retrieve and display user data from a database, while the second example shows how to insert a new employee record into an employee table. Both examples include steps for establishing a database connection and handling errors.

Uploaded by

ARYAN MOHADE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

PRACTICAL No.

15
Outputs

Q.1 Develop a program to retrieve and present data from database.

Ans.

<?php
// Step 1: Database connection
$conn = new mysqli("localhost", "root", "", "test_db");
// Step 2: Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Step 3: Retrieve data
$sql = "SELECT id, name, email FROM users";
$result = $conn->query($sql);
// Step 4: Display data
if ($result->num_rows > 0) {
echo "<table
border='1'><tr><th>ID</th><th>Name</th><th>Email</th></tr>";
while($row = $result->fetch_assoc()) {
echo
"<tr><td>{$row["id"]}</td><td>{$row["name"]}</td><td>{$row["email"]}</td>
</tr>";
}
echo "</table>"; Ouput
} else {
echo "No data found.";
}

// Step 5: Close connection


$conn->close();
?>
PRACTICAL No.15
Outputs

Q.2 Write a php code to insert data into employee table.

Ans.

<?php
// Database connection
$conn = new mysqli("localhost", "root", "", "company");

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// Sample data to insert (example not related to you)


$name = "John Doe";
$position = "Software Engineer";
$salary = 70000;

// SQL insert query


$sql = "INSERT INTO employee (name, position, salary) VALUES ('$name',
'$position', $salary)";

// Execute and check


if ($conn->query($sql) === TRUE) {
echo "Employee added successfully!";
} else {
echo "Error: " . $conn->error; Ouput
}
Employee added
$conn->close(); successfully!
?>

You might also like