Develop a PHP script that fetches JSON data from a given API endpoint and saves it into a MySQL database. The script should use prepared statements to prevent SQL injection attacks and handle any errors gracefully.

To create a PHP script that fetches JSON data from an API endpoint and saves it into a MySQL database, follow these steps:

1. Create a new PHP file (e.g., `fetch_and_save_data.php`).

2. Add the following code to connect to your MySQL database using PDO:

```php
<?php

$host = 'localhost';
$dbname = 'your_database_name';
$user = 'your_username';
$password = 'your_password';

try {
    $conn = new PDO("mysql:host=$host;dbname=$dbname", $user, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}
```

Replace `'your_database_name'`, `'your_username'`, and `'your_password'` with your actual database credentials.

3. Define the API endpoint URL:

```php
$api_url = 'https://api.example.com/data'; // Replace this with the actual API endpoint.
```

4. Fetch the JSON data from the API endpoint using `file_get_contents()` or cURL:

```php
$json_data = file_get_contents($api_url);

if ($json_data === false) {
    die('Error fetching data from API.');
}

$data_array = json_decode($json_data, true);

if ($data_array === null) {
    die('Error decoding JSON data.');
}
```

5. Prepare an SQL statement to insert the fetched data into the database. This example assumes you have a table named `my_table` with columns `column1`, `column2`, and `column3`. Modify the table name and column names according to your schema:

```php
$sql = "INSERT INTO my_table (column1, column2, column3) VALUES (:column1, :column2, :column3)";
$stmt = $conn->prepare($sql);
```

6. Loop through the fetched data and bind values to the prepared statement, then execute it:

```php
foreach ($data_array as $item) {
    // Assuming the JSON object has keys 'key1', 'key2', and 'key3' that correspond to your table columns.
    $stmt->bindValue(':column1', $item['key1']);
    $stmt->bindValue(':column2', $item['key2']);
    $stmt->bindValue(':column3', $item['key3']);

    try {
        $stmt->execute();
    } catch (PDOException $e) {
        echo "Error inserting data: " . $e->getMessage();
    }
}
```

7. Close the database connection:

```php
$conn = null;
```

The complete script should look like this:

```php
<?php

$host = 'localhost';
$dbname = 'your_database_name';
$user = 'your_username';
$password = 'your_password';

try {
    $conn = new PDO("mysql:host=$host;dbname=$dbname", $user, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}

$api_url = 'https://api.example.com/data'; // Replace this with the actual API endpoint.

$json_data = file_get_contents($api_url);

if ($json_data === false) {
    die('Error fetching data from API.');
}

$data_array = json_decode($json_data, true);

if ($data_array === null) {
    die('Error decoding JSON data.');
}

$sql = "INSERT INTO my_table (column1, column2, column3) VALUES (:column1, :column2, :column3)";
$stmt = $conn->prepare($sql);

foreach ($data_array as $item) {
    // Assuming the JSON object has keys 'key1', 'key2', and 'key3' that correspond to your table columns.
    $stmt->bindValue(':column1', $item['key1']);
    $stmt->bindValue(':column2', $item['key2']);
    $stmt->bindValue(':column3', $item['key3']);

    try {
        $stmt->execute();
    } catch (PDOException $e) {
        echo "Error inserting data: " . $e->getMessage();
    }
}

$conn = null;
```

Now, when you run this script, it will fetch JSON data from the specified API endpoint and insert it into your MySQL database using prepared statements.