0% found this document useful (0 votes)
2 views

Exerciții PHP MYSQL

The document provides PHP scripts for connecting to a MySQL database and performing basic SQL operations: INSERT, DELETE, UPDATE, and SELECT. Each script establishes a connection to the database, executes the corresponding SQL command, and handles potential errors. The SELECT operation also formats the results into an HTML table for display.

Uploaded by

IOAN DOROȘ
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Exerciții PHP MYSQL

The document provides PHP scripts for connecting to a MySQL database and performing basic SQL operations: INSERT, DELETE, UPDATE, and SELECT. Each script establishes a connection to the database, executes the corresponding SQL command, and handles potential errors. The SELECT operation also formats the results into an HTML table for display.

Uploaded by

IOAN DOROȘ
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Exerciții: Aplicații de conectare la bazele de date MySQL

Pentru accesarea bazelor de date, de tip MySQL, din scripturi PHP, se vor scrie 4 scripturi,
pentru principalele comenzi SQL
- INSERT
- DELETE
- UPDATE
- SELECT
Rezultatul comenzii SELECT va fi raportat într-un tabel folosindu-se comenzi HTML și CSS.
<!-- Script pentru implementare INSERT-->
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "nwjns";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO Remixes (SongID, RemixedSong, Producer, SongType)


VALUES ('12', 'Super Shy', 'FRNK Remix', 'Song')";

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


echo "New song added successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>

<!-- Script pentru implementare DELETE-->


<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "nwjns";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// sql to delete a record


$sql = "DELETE FROM remixes WHERE SongID=8";
$sql = "DELETE FROM remixes WHERE RemixedSong='Cookie'";

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


echo "Record deleted successfully";
} else {
echo "Error deleting record: " . $conn->error;
}
$conn->close();
?

<!-- Script pentru implementare UPDATE-->


<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "nwjns";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "UPDATE Singles SET SongPeak=16 WHERE SongID=4";


$sql = "UPDATE Singles SET SongPeak=24 WHERE SongTitle='ASAP'";

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


echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
?>

<!-- Script pentru implementare SELECT-->


<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "nwjns";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT SongTitle, SongYear, AlbumTitle FROM singles";


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

<!DOCTYPE html>
<html>
<head>
<title> Fetch Data From Database </title>
</head>
<body>
<table align="center" border="1px" style="width:600px;line-height:40px;">
<tr>
<th colspan="4"><h2>Student Record</h2></th>
</tr>
<th> Song </th>
<th> Year </th>
<th> Album </th>
</tr>

// output data of each row


<?php while($row = $result->fetch_assoc()) {
?>
<tr>
<td><?php echo $row['SongTitle']; ?></td>
<td><?php echo $row['SongYear']; ?></td>
<td><?php echo $row['AlbumTitle']; ?></td>
</tr>
<?php
}

$conn->close();
?>

</table>
</body>
</html

You might also like