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

# Connecting to SQLite using PHP

> Learn How To Connect To SQLite With PHP. Our Step-By-Step PHP SQLite Guide Makes It Extremely Easy!

This article describes how to connect to a SQLite database using PHP.

<Note>
  The SQLite database must already exist before you can use the following method. For information about how to create a SQLite database, please see [this article](/docs/connect-to-sqlite-from-the-command-line).
</Note>

## Connecting to SQLite using PDO (PHP Data Objects)

PDO (PHP Data Objects) abstracts database access and enables you to create code that can handle different types of databases. One of the database types that PDO supports is SQLite.

<Note>
  There is a set of legacy PHP SQLite functions whose names start with **sqlite\_** (for example, **sqlite\_open**). These functions only support SQLite2. You must use PDO to access SQLite3 databases.
</Note>

To connect to SQLite using PDO, follow these steps:

1. Use the following PHP code to connect to the SQLite database. Replace ***username*** with your hosting.com account username, ***path*** with the path to the database file, and ***filename*** with the name of the database file:

   ```php theme={null}
   <?php
       $myPDO = new PDO('sqlite:/home/username/path/filename');
   ?>
   ```

   > 📘 Note
   >
   > For example, if you have a SQLite database named *books.db* in your home directory, and your username is *example*, you would use the following statement:
   >
   > ```php theme={null}
   > <?php
   >     $myPDO = new PDO('sqlite:/home/example/books.db');
   > ?>
   > ```

2. After the code connects to the SQLite database, you can run SQL queries and perform other operations. For example, the following PHP code runs a SQL query that extracts the last names from the *employees* table, and stores the result in the *\$result* variable:

   ```php theme={null}
   <?php
       $result = $myPDO->query("SELECT lastname FROM employees");
   ?>
   ```

   > 👍 Tip
   >
   > Here is one way to access the result set's values and print them:
   >
   > ```php theme={null}
   > <?php
   >     foreach($result as $row)
   >     {
   >         print $row['lastname'] . "\n";
   >     }
   > ?>
   > ```

## More information

For more information about PDO, please visit [http://www.php.net/manual/en/book.pdo.php](http://www.php.net/manual/en/book.pdo.php).

## Related articles

* [Connecting to SQLite from the command line](/docs/connect-to-sqlite-from-the-command-line)
