0% found this document useful (0 votes)
29 views8 pages

Practical No 15&16

Uploaded by

codingallhere
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)
29 views8 pages

Practical No 15&16

Uploaded by

codingallhere
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/ 8

Practical no 15&16:

Create table in php

<?php

$servername = "localhost";

$username = "root";

$password = "";

// Create connection

$conn = new mysqli($servername, $username, $password);

// Check connection

if ($conn->connect_error) {

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

// Create database

$sql = "CREATE DATABASE myDB";

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

echo "Database created successfully";

} else {

echo "Error creating database: " . $conn->error;

$conn->close();

?>
<?php

$servername = "localhost";

$username = "root";

$password = "";

$database = "mydatabase";

// Create connection

$conn = new mysqli($servername, $username, $password);

// Check connection

if ($conn->connect_error) {

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

// Create database

$sql_create_db = "CREATE DATABASE IF NOT EXISTS $database";

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

echo "Database created successfully<br>";

} else {
echo "Error creating database: " . $conn->error . "<br>";

// Select database

$conn->select_db($database);

// Create table

$sql_create_table = "CREATE TABLE IF NOT EXISTS mytable (

id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,

firstname VARCHAR(30) NOT NULL,

lastname VARCHAR(30) NOT NULL,

email VARCHAR(50),

reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

)";

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

echo "Table created successfully<br>";

} else {

echo "Error creating table: " . $conn->error . "<br>";

$conn->close();

?>
<?php

$servername = "localhost";

$username = "root";

$password = "";

$database = "mydatabase";

// Create connection

$conn = new mysqli($servername, $username, $password, $database);

// Check connection

if ($conn->connect_error) {

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

// INSERT operation

$sql_insert = "INSERT INTO mytable (firstname, lastname, email) VALUES ('John', 'Doe',
'[email protected]')";

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

echo "New record inserted successfully<br>";

} else {

echo "Error inserting record: " . $conn->error . "<br>";

}
$conn->close();

?>

<?php

$servername = "localhost";

$username = "root";

$password = "";

$database = "mydatabase";

// Create connection

$conn = new mysqli($servername, $username, $password, $database);


// Check connection

if ($conn->connect_error) {

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

// SELECT operation

$sql_select = "SELECT id, firstname, lastname, email FROM mytable";

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

if ($result->num_rows > 0) {

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

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

} else {

echo "0 results<br>";

$conn->close();

?>
<?php

$servername = "localhost";

$username = "root";

$password = "";

$database = "mydatabase";

// Create connection

$conn = new mysqli($servername, $username, $password, $database);

// Check connection

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

// DELETE operation

$sql_delete = "DELETE FROM mytable WHERE firstname='John'";

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

echo "Record deleted successfully<br>";

} else {

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

$conn->close();

?>

You might also like