//connect to the database $conn = new PDO("mysql:host=localhost;dbname=mydatabase", "username", "password"); //prepare the query $stmt = $conn->prepare("INSERT INTO users (name, email) VALUES (:name, :email)"); //bind the parameters $stmt->bindParam(':name', $name); $stmt->bindParam(':email', $email); //set the values of the parameters $name = "John Doe"; $email = "johndoe@example.com"; //execute the query $stmt->execute();
//connect to the database $conn = new PDO("mysql:host=localhost;dbname=mydatabase", "username", "password"); //prepare the query $stmt = $conn->prepare("INSERT INTO users (name, email) VALUES (?, ?)"); //define an array of values to be inserted $users = array( array("John Doe", "johndoe@example.com"), array("Jane Doe", "janedoe@example.com"), array("Bob Smith", "bobsmith@example.com") ); //loop through the array and execute the query for each record foreach($users as $user) { $stmt->execute($user); }In this example, the function is used to insert multiple records into a table named 'users'. The values for the 'name' and 'email' columns are supplied through an array of values. The function is executed for each record in the array.