This article describes how to connect to a SQLite database using PHP.
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.
To connect to SQLite using PDO, follow these steps:
<?php $myPDO = new PDO('sqlite:/home/username/path/filename'); ?>
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 $myPDO = new PDO('sqlite:/home/example/books.db'); ?>
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 $result = $myPDO->query("SELECT lastname FROM employees"); ?>
Here is one way to access the result set's values and print them:
<?php foreach($result as $row) { print $row['lastname'] . "\n"; } ?>
For more information about PDO, please visit http://www.php.net/manual/en/book.pdo.php.
Subscribe to receive weekly cutting edge tips, strategies, and news you need to grow your web business.
No charge. Unsubscribe anytime.
Did you find this article helpful? Then you'll love our support. Experience the A2 Hosting difference today and get a pre-secured, pre-optimized website. Check out our web hosting plans today.
Usamos cookies con el fin de personalizar para usted nuestro sitio Web y analizar el uso de éste. Usted acepta esto haciendo clic en “Acepto” o al continuar usando el sitio Web. Hay más información sobre cookies en nuestra política de privacidad.