> ## Documentation Index
> Fetch the complete documentation index at: https://darkstation.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Local Connection

> Connect LibSQL Local databases using the **LibSQL PHP Extension**

Local databases connection using the **LibSQL PHP Extension**, there are several straightforward options available. Let's explore each option in detail:

## Standard DSN Connection

This option involves using a Data Source Name (DSN) string to connect to the desired local database.

```php theme={null}
$db = new LibSQL("libsql:dbname=database.db");
```

* **Usage Explanation:**
  * The DSN string format is `libsql:dbname=database.db`, where `"database.db"` represents the name of the local database file.
  * This option is suitable for connecting to a database using the LibSQL protocol with the specified database name.

## Standard SQLite Connection

In this option, you directly specify the filename of the SQLite database without using a DSN string.

```php theme={null}
$db = new LibSQL("database.db");
```

* **Usage Explanation:**
  * Here, `"database.db"` is the filename of the SQLite database.
  * This option simplifies the connection process by directly referencing the database file.

## Standard LibSQL Connection

This option is similar to the first option but uses the file protocol in the DSN string.

```php theme={null}
$db = new LibSQL("file:database.db");
```

* **Usage Explanation:**
  * The DSN string format is `file:database.db`, where `"database.db"` is the name of the local database file.
  * It allows for connecting to a database using the LibSQL protocol with the specified file name.

## Usage Example

```php theme={null}
<?php

$db = new LibSQL("file:database.db");

// Create table
$sql = "CREATE TABLE IF NOT EXISTS users (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  name TEXT, 
  age INTEGER
)";
$db->execute($sql);

// Insert data
$db->execute("INSERT INTO users (name, age) VALUES ('Diana Hooggan', 24)");

// Read data
$results = $db->query("SELECT * FROM users");

// Display data
foreach ($results['rows'] as $row) {
    echo "ID: " . $row['id'] . ", Name: " . $row['name'] . ", Age: " . $row['age'] . "\n";
}

// Close database
$db->close();
```

***

## Error Handling

It's important to note that incorrect usage of the connection parameters may result in errors. For example:

```php theme={null}
// Error: PHP Fatal error:  Uncaught Exception: Failed to parse DSN
$db = new LibSQL("libsql:database.db");

// Error: PHP Fatal error:  Uncaught Exception: Failed to parse DSN
$db = new LibSQL("");
```

* **Error Explanation:**
  * Providing an invalid DSN string, such as missing required parameters or an empty string, will result in a parsing error.
  * Ensure that the DSN string is correctly formatted and contains the necessary parameters for establishing a connection.

By following these usage guidelines and ensuring the correct format of the DSN string, you can effectively establish connections with local databases using the **LibSQL PHP Extension**.
