Remote Connection
Connect LibSQL Remote databases using the LibSQL PHP Extension
When establishing connections to remote databases using the LibSQL PHP Extension, you have two options available. Both options utilize the LibSQL class constructor to create a new database connection object.
Configuration
To create remote connection you need to prepare your Turso/LibSQL Database DATABASE_URL
and DATABASE_AUTH_TOKEN
.
If you already have Turso/LibSQL Database account, do following this commands:
Get the database URL:
Get the database authentication token:
Assign credentials to the environment variables inside .env
or somewhere else.
If you doesn’t have Turso/LibSQL Database Account, create one here it’s free for individual developers just getting started!
- 500 Databases
- 9GB of total storage
- 1 billion row reads
- Unlimited Embedded Replicas
See for other plan Turso Database Pricing.
Connecting to a remote database is equally effortless. Choose between 2 options:
-
Standard DSN Connection with
libsql://
: -
Standard DSN Connection with
https://
:
For remote connections, this option utilizes the libsql://
or https://
protocol in the DSN string. The format is:
It provides a straightforward approach to connect to remote databases.
Here the usage implementation:
Connect with libsql://
Protocol
-
Usage Explanation:
- This option employs the
libsql://
protocol in the DSN string to connect to the remote database. - The DSN string specifies the database name as
libsql://database-org.turso.io
. - Additionally, it requires an authentication token (
authToken
) for secure access, provided asrandom-token
.
- This option employs the
-
Steps to Use:
- Replace
"libsql://database-org.turso.io"
with the actual database name or path you intend to connect to. - Ensure you have the correct authentication token (
authToken
) for accessing the database securely. - Use this connection object (
$db
) to interact with the specified remote database in your PHP application.
- Replace
Connect with https://
Protocol
-
Usage Explanation:
- Similar to Option 1, this option utilizes a DSN string with the
https://
protocol to establish a connection. - The DSN string specifies the database name or path as
https://database-org.turso.io
. - It also requires an authentication token (
authToken
) provided asrandom-token
for secure access.
- Similar to Option 1, this option utilizes a DSN string with the
-
Steps to Use:
- Replace
"https://database-org.turso.io"
with the actual database name or path you wish to connect to. - Ensure you have the correct authentication token (
authToken
) for accessing the database securely. - Utilize the created connection object (
$db
) to interact with the designated remote database within your PHP application.
- Replace
By following the steps outlined above, you can seamlessly establish remote connections to databases using either the libsql://
or https://
protocols with the LibSQL PHP Extension. Ensure accurate configuration of the DSN string and authentication token for successful connection establishment and secure data access.
Usage Example
Was this page helpful?