Write down the server-side script to create a database, connect with it, create a table, and insert data in it.

Table of Contents
    <?php

// Database configuration
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mydatabase";

// Create connection
$conn = mysqli_connect($servername, $username, $password);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

// Create database
$sql = "CREATE DATABASE IF NOT EXISTS $dbname";
if (mysqli_query($conn, $sql)) {
    echo "Database created successfully\n";
} else {
    echo "Error creating database: " . mysqli_error($conn) . "\n";
}

// Select database
mysqli_select_db($conn, $dbname);

// Create table
$sql = "CREATE TABLE IF NOT EXISTS users (
    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(30) NOT NULL,
    email VARCHAR(50) NOT NULL,
    password VARCHAR(255) NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)";
if (mysqli_query($conn, $sql)) {
    echo "Table created successfully\n";
} else {
    echo "Error creating table: " . mysqli_error($conn) . "\n";
}

// Insert data
$sql = "INSERT INTO users (name, email, password)
    VALUES ('John Doe', 'johndoe@example.com', 'password123')";
if (mysqli_query($conn, $sql)) {
    echo "Data inserted successfully\n";
} else {
    echo "Error inserting data: " . mysqli_error($conn) . "\n";
}

// Close connection
mysqli_close($conn);

?>

About the Author

A free online educational resource provider.

Post a Comment

Please do not enter any spam link in the comment box.

Frequently Asked Questions

What is Nepali Educate?

Nepali Educate is an online platform dedicated to providing educational resources, support, and information for students, parents, and educators in Nepal.

What services does Nepali Educate offer?

Nepali Educate offers a range of services, including educational articles, exam preparation resources, career guidance, and information about educational institutions in Nepal.

How can I access the resources on Nepali Educate?

All resources on Nepali Educate are accessible through the website. Simply visit the website and explore the various sections, including articles, exam tips, and career guidance.

Are the resources on Nepali Educate free?

Yes, the majority of the resources on Nepali Educate are available for free. However, there might be some premium or additional services that require a subscription or payment.

How can I contribute to Nepali Educate?

Nepali Educate welcomes contributions from educators, professionals, and students. If you have valuable insights, educational content, or resources to share, you can contact us through the website for contribution opportunities.

Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.