পিএইচপি – মাইএসকিউএল এ একাধিক রেকর্ড প্রবেশ করানো (PHP – Insert Multiple Records Into MySQL in Bangla)

মোঃ আরিফুল ইসলাম

MySQLi এবং PDO ব্যবহার করে মাইএসকিউএল এর মধ্যে একাধিক ডাটা রেকর্ডস প্রবেশ করান

একাধিক SQL statements কে executed (উদ্দিপ্ত) করার জন্য mysqli_multi_query() function (ফাংশন) ব্যাবহার করতে হবে।

নিম্নলিখিত উদাহরণগুলি "MyGuests" টেবিল এ তিনটি নতুন রেকর্ড যোগ করাবে:

MySQLi Object-oriented এর উদাহরণ-


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

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com');";
$sql .= "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('Mary', 'Moe', 'mary@example.com');";
$sql .= "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('Julie', 'Dooley', 'julie@example.com')";

if ($conn->multi_query($sql) === TRUE) {
    echo "New records created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

 

MySQLi Procedural এর উদাহরণ-


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

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com');";
$sql .= "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('Mary', 'Moe', 'mary@example.com');";
$sql .= "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('Julie', 'Dooley', 'julie@example.com')";

if (mysqli_multi_query($conn, $sql)) {
    echo "New records created successfully";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

mysqli_close($conn);
?>

 

PDO এর উপায়টা সামান্য একটু ভিন্ন-

PDO এর উদাহরণ-


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

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // begin the transaction
    $conn->beginTransaction();
    // our SQL statememtns
    $conn->exec("INSERT INTO MyGuests (firstname, lastname, email)
    VALUES ('John', 'Doe', 'john@example.com')");
    $conn->exec("INSERT INTO MyGuests (firstname, lastname, email)
    VALUES ('Mary', 'Moe', 'mary@example.com')");
    $conn->exec("INSERT INTO MyGuests (firstname, lastname, email)
    VALUES ('Julie', 'Dooley', 'julie@example.com')");

    // commit the transaction
    $conn->commit();
    echo "New records created successfully";
    }
catch(PDOException $e)
    {
    // roll back the transaction if something failed
    $conn->rollback();
    echo "Error: " . $e->getMessage();
    }

$conn = null;
?>

 

Permanent link to this article: http://bangla.sitestree.com/%e0%a6%aa%e0%a6%bf%e0%a6%8f%e0%a6%87%e0%a6%9a%e0%a6%aa%e0%a6%bf-%e0%a6%ae%e0%a6%be%e0%a6%87%e0%a6%8f%e0%a6%b8%e0%a6%95%e0%a6%bf%e0%a6%89%e0%a6%8f%e0%a6%b2-%e0%a6%8f-%e0%a6%8f%e0%a6%95%e0%a6%be/