Php mysql
CREATE DATABASE studentDB;
USE studentDB;
CREATE TABLE students (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100),
age INT,
course VARCHAR(100)
);
<?php
// Step 1: Connect to MySQL
$host = "localhost";
$user = "root";
$pass = ""; // Your MySQL password
$db = "studentDB";
$conn = new mysqli($host, $user, $pass, $db);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Step 2: Prepare the statement
$stmt = $conn->prepare("INSERT INTO students (name, email, age, course) VALUES (?, ?, ?, ?)");
$stmt->bind_param("ssis", $name, $email, $age, $course);
// Step 3: Insert 5 records
$students = [
["Adnan", "adnan@example.com", 21, "Computer Science"],
["Sara", "sara@example.com", 22, "Electronics"],
["Ravi", "ravi@example.com", 20, "Mechanical"],
["Priya", "priya@example.com", 23, "Civil"],
["John", "john@example.com", 19, "IT"]
];
foreach ($students as $s) {
[$name, $email, $age, $course] = $s;
$stmt->execute();
}
echo "5 records inserted successfully!";
$stmt->close();
$conn->close();
?>
Comments
Post a Comment