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

Amaan 11

The document contains PHP code snippets for managing a MySQL database, including deleting a record from a 'Students' table, creating a new database, creating a 'Users' table, and inserting records. It demonstrates the use of both mysqli and PDO for database operations. Error handling is included to manage potential issues during these operations.

Uploaded by

kccollege0123
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)
15 views2 pages

Amaan 11

The document contains PHP code snippets for managing a MySQL database, including deleting a record from a 'Students' table, creating a new database, creating a 'Users' table, and inserting records. It demonstrates the use of both mysqli and PDO for database operations. Error handling is included to manage potential issues during these operations.

Uploaded by

kccollege0123
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

echo "0 results";

mysqli_close($conn);
?>

Delete:

<?php
$conn = mysqli_connect("localhost", "root", "", "FYCS24");

$sql = "DELETE FROM Students WHERE id=1";

if (mysqli_query($conn, $sql)) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . mysqli_error($conn);
}

mysqli_close($conn);
?>

PHP Data Objects (PDO)


Creating database:

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

try {
$conn = new PDO("mysql:host=$servername", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "CREATE DATABASE KCFYCS63";
// use exec() because no results are returned
$conn->exec($sql);
echo "Database created successfully<br>";
} catch(PDOException $e) {
echo $sql . "<br>" . $e->getMessage();
}

$conn = null;
?>

Creating table:

<?php
try {
$conn = new PDO("mysql:host=localhost;dbname=KCFYCS64", "root", "");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$sql = "CREATE TABLE Users (


id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50)
)";

$conn->exec($sql);
echo "Table Users created successfully";
} catch(PDOException $e) {
echo "Error creating table: " . $e->getMessage();
}
$conn = null;
?>

Inserting record:

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

try {

You might also like