Lab 9

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Lab 9

Connecting PHP with MYSQL

Select Data from Table Stored in The Database:

<!DOCTYPE html>
<html>
<body>

<?php
$user = 'root';
$password = '';
$dbname = "myDB4";

// Create connection
$conn = new mysqli('127.0.0.1', $user, $password,$dbname);

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

$sql = "SELECT id, firstname, lastname FROM MyGuests";


$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row

Eng. Areeg Ahmed Bukair


Web Applications Development
B3IT
while($row = $result->fetch_assoc()) {
echo "<br> id: ". $row["id"]. " - Name: ".
$row["firstname"]. " " . $row["lastname"] . "<br>";
}
} else {
echo "0 results";
}

$conn->close();
?>
</body>

Select Data from Table Stored in The Database and Put it


in HTML Table:

</html>
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
}

Eng. Areeg Ahmed Bukair


Web Applications Development
B3IT
</style>
</head>
<body>

<?php
$user = 'root';
$password = '';
$dbname = "myDB4";

// Create connection
$conn = new mysqli('127.0.0.1', $user, $password,$dbname);

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

$sql = "SELECT id, firstname, lastname FROM MyGuests";


$result = $conn->query($sql);

if ($result->num_rows > 0) {
echo "<table><tr><th>ID</th><th>Name</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["id"]. "</td><td>" .
$row["firstname"]. " " . $row["lastname"]. "</td></tr>";
}
echo "</table>";

Eng. Areeg Ahmed Bukair


Web Applications Development
B3IT
} else {
echo "0 results";
}

$conn->close();
?>

</body>
</html>

Select and Filter Data:


The following example selects the id, firstname and lastname columns from the MyGuests table
where the lastname is "Doe", and displays it on the page:

<!DOCTYPE html>

<html>

<body>

<?php
Eng. Areeg Ahmed Bukair
Web Applications Development
B3IT
$user = 'root';

$password = '';

$dbname = "myDB4";

// Create connection

$conn = new mysqli('127.0.0.1', $user, $password,$dbname);

// Check connection

if ($conn->connect_error) {

die("Connection failed: " . $conn->connect_error);

$sql = "SELECT id, firstname, lastname FROM MyGuests WHERE lastname='Doe'";

$result = $conn->query($sql);

if ($result->num_rows > 0) {

// output data of each row

while($row = $result->fetch_assoc()) {

echo "<br> id: ". $row["id"]. " - Name: ". $row["firstname"]. " " . $row["lastname"] . "<br>";

} else {

echo "0 results";

$conn->close();

?>

</body>

</html>

Eng. Areeg Ahmed Bukair


Web Applications Development
B3IT
Delete Data From a MySQL Table
The following examples delete the record with id=3 in the "MyGuests" table:

<?php

$user = 'root';

$password = '';

$dbname = "myDB4";

// Create connection

$conn = new mysqli('127.0.0.1', $user, $password,$dbname);

// Check connection

if ($conn->connect_error) {

die("Connection failed: " . $conn->connect_error);

// sql to delete a record

$sql = "DELETE FROM MyGuests WHERE id=3";

if ($conn->query($sql) === TRUE) {

echo "Record deleted successfully";

} else {

echo "Error deleting record: " . $conn->error;

$conn->close();

?>
Eng. Areeg Ahmed Bukair
Web Applications Development
B3IT

You might also like