What is the best way to execute SQL scripts in PHP for database inserts?

When executing SQL scripts in PHP for database inserts, it is best to use prepared statements to prevent SQL injection attacks and improve performance. Prepared statements separate SQL code from user input, making it safer and more efficient. To execute SQL scripts using prepared statements in PHP, you can use PDO (PHP Data Objects) or mysqli functions.

// Create a PDO connection to the database
$dsn = 'mysql:host=localhost;dbname=mydatabase';
$username = 'username';
$password = 'password';
try {
    $pdo = new PDO($dsn, $username, $password);
} catch (PDOException $e) {
    die('Connection failed: ' . $e->getMessage());
}

// Prepare and execute a SQL insert statement
$stmt = $pdo->prepare('INSERT INTO mytable (column1, column2) VALUES (:value1, :value2)');
$value1 = 'some value';
$value2 = 'another value';
$stmt->bindParam(':value1', $value1);
$stmt->bindParam(':value2', $value2);
$stmt->execute();