<?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); ?>
Write down the server-side script to create a database, connect with it, create a table, and insert data in it.
Getting Info...