Skip to content

Commit

Permalink
[docs][cloud] php sample app (#11505)
Browse files Browse the repository at this point in the history
  • Loading branch information
ddhodge authored Feb 18, 2022
1 parent b4215a5 commit 2b8e813
Show file tree
Hide file tree
Showing 16 changed files with 244 additions and 52 deletions.
10 changes: 9 additions & 1 deletion docs/content/latest/reference/drivers/ysql-client-drivers.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,20 @@ $ npm install pg

The [php-pgsql](https://www.php.net/manual/en/book.pgsql.php) driver is a collection of the official PostgreSQL module for PHP. php-pgsql is based on [libpq](#libpq) and supports the [SCRAM-SHA-256 authentication method](../../../secure/authentication/password-authentication/#scram-sha-256).

For a tutorial on building a sample Node.js application with php-pgsql, see [Build a Node.js application](../../../quick-start/build-apps/php/ysql/).
For details on installing and using php-pgsql, see the [php-pgsql documentation](https://www.php.net/manual/en/book.pgsql.php).

For a tutorial on building a sample PHP application with php-pgsql, see [Build a PHP application](../../../quick-start/build-apps/php/ysql/).

#### Install the php-pgsql driver

To enable PostgreSQL support using php-pgsql, see [Installing/Configuring](https://www.php.net/manual/en/pgsql.setup.php) in the PHP documentation.

Homebrew users on macOS can install PHP using `brew install php`; the php-pgsql driver is installed automatically.

Ubuntu users can install the driver using the `sudo apt-get install php-pgsql` command.

CentOS users can install the driver using the `sudo yum install php-pgsql` command.

## Python

### psycopg2
Expand Down
2 changes: 1 addition & 1 deletion docs/content/latest/yugabyte-cloud/cloud-basics/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Before deploying a cluster, you need to decide on the following:
<div class="col-12 col-md-6 col-lg-12 col-xl-6">
<a class="section-link icon-offset" href="create-clusters-overview/">
<div class="head">
<img class="icon" src="/images/section_icons/quick_start/create_cluster.png" aria-hidden="true" />
<img class="icon" src="/images/section_icons/introduction/benefits.png" aria-hidden="true" />
<div class="title">Plan your cluster</div>
</div>
<div class="body">
Expand Down
4 changes: 2 additions & 2 deletions docs/content/latest/yugabyte-cloud/cloud-connect/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Once you have connected to the database, you can add database users for other te
<div class="col-12 col-md-6 col-lg-12 col-xl-6">
<a class="section-link icon-offset" href="connect-cloud-shell/">
<div class="head">
<img class="icon" src="/images/section_icons/manage/backup.png" aria-hidden="true" />
<img class="icon" src="/images/section_icons/explore/cloud_native.png" aria-hidden="true" />
<div class="title">Cloud shell</div>
</div>
<div class="body">
Expand All @@ -42,7 +42,7 @@ Once you have connected to the database, you can add database users for other te
<div class="col-12 col-md-6 col-lg-12 col-xl-6">
<a class="section-link icon-offset" href="connect-client-shell/">
<div class="head">
<img class="icon" src="/images/section_icons/manage/backup.png" aria-hidden="true" />
<img class="icon" src="/images/section_icons/index/develop.png" aria-hidden="true" />
<div class="title">Client shell</div>
</div>
<div class="body">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ isTocNested: true
showAsideToc: true
---

Applications connect to and interact with YugabyteDB using API client libraries, also called client drivers. Before you can connect an application, you need to install the correct driver. For information on available drivers, refer to [Build an application](../../../quick-start/build-apps).
Applications connect to and interact with YugabyteDB using API client libraries, also called client drivers. Before you can connect an application, you need to install the correct driver.

For examples of connecting applications to Yugabyte Cloud, refer to [Develop](../../cloud-develop/).
Yugabyte Cloud clusters have SSL (encryption in-transit) enabled so make sure your driver details include SSL parameters.

For examples of applications that connect to Yugabyte Cloud using common drivers, refer to [Build an application](../../cloud-quickstart/cloud-build-apps).

Clusters have SSL (encryption in-transit) enabled so make sure your driver details include SSL parameters.
For examples of connecting applications to Yugabyte Cloud, refer to [Develop](../../cloud-develop/).

{{< note title="Note" >}}

Expand Down Expand Up @@ -104,7 +106,7 @@ To run the sample application:
1. Run the command in docker from your computer, replacing `<path to CA cert>`, `<db user>`, and `<db password>` with the path to the CA certificate for the cluster and your database credentials.
-->

## Next steps
## Learn more

- [Add database users](../../cloud-secure-clusters/add-users/)
- [Develop applications](../../../develop/)
- [Build an application](../../cloud-quickstart/cloud-build-apps)
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ Once connected, Yugabyte Cloud adds one or more entries for the cloud shell sess

## ysqlsh meta-commands in cloud shell

Cloud shell supports the use of ysqlsh [meta-commands](../../../admin/ysqlsh/#meta-commands). However, for security reasons, some commands are not available. With the exception of read-only access to the `/share` directory to load the sample datasets, commands that access the filesystem do not work in cloud shell.
Cloud shell supports the use of ysqlsh [meta-commands](../../../admin/ysqlsh/#meta-commands). However, for security reasons, some commands are not available. With the exception of read-only access to the `/share` directory to load the [sample datasets](../../../sample-data/), commands that access the filesystem do not work in cloud shell.

The following table lists common meta-commands that can be used in cloud shell.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@ showAsideToc: true

Review activity on your cluster using the **Activity** tab. The tab lists cluster activity, including the activity type, status, and start and end time.

To review activity for your entire cloud, use the [**Activity** tab](../../cloud-secure-clusters/cloud-activity/) on the **Admin** page.

![Cloud Cluster Activity tab](/images/yb-cloud/cloud-clusters-activity.png)

## Logged activity

The following table lists the cluster activity that is logged.

| Source | Activity |
| --- | --- |
| Cluster | Create cluster<br>Edit cluster |
| Allow list | Edit allow list |
| Backup | Create backup<br>Delete backup<br>Restore backup |
| Cluster | Create cluster<br>Edit cluster<br>Upgrade cluster<br>Pause cluster<br>Resume cluster |
| Allow List | Edit IP Allow Lists |
<!-- | Backup | Create backup<br>Delete backup<br>Restore backup | -->
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,18 @@ For more advanced applications, including Spring and GraphQL examples, refer to
</a>
</div>

<div class="col-12 col-md-6 col-lg-12 col-xl-6">
<a class="section-link icon-offset" href="cloud-ysql-php/">
<div class="head">
<div class="icon">
<i class="icon-php"></i>
</div>
<div class="title">PHP</div>
</div>
<div class="body">
PHP application that connects to a YugabyteDB cluster using the php-pgsql driver.
</div>
</a>
</div>

</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
---
title: Build a PHP application that uses YSQL
headerTitle: Build a PHP application
description: Build a small PHP application using the php-pgsql driver and using the YSQL API to connect to and interact with a Yugabyte Cloud cluster.
menu:
latest:
parent: cloud-build-apps
name: PHP
identifier: cloud-php
weight: 900
type: page
isTocNested: true
showAsideToc: true
---

The following tutorial shows a small [PHP application](https://github.com/yugabyte/yugabyte-simple-php-app) that connects to a YugabyteDB cluster using the [php-pgsql](../../../../reference/drivers/ysql-client-drivers/#php-pgsql) driver and performs basic SQL operations. Use the application as a template to get started with Yugabyte Cloud in PHP.

## Prerequisites

- PHP runtime. The sample application was created using PHP 8.1 but should work with earlier and later versions. Homebrew users on macOS can install PHP using `brew install php`.
- [php-pgsql driver](../../../../reference/drivers/ysql-client-drivers/#php-pgsql).
- On macOS, Homebrew automatically installs the driver with `brew install php`.
- Ubuntu users can install the driver using the `sudo apt-get install php-pgsql` command.
- CentOS users can install the driver using the `sudo yum install php-pgsql` command.

### Yugabyte Cloud

- You have a cluster deployed in Yugabyte Cloud. To get started, use the [Quick start](../../).
- You downloaded the cluster CA certificate. Refer to [Download your cluster certificate](../../../cloud-secure-clusters/cloud-authentication/#download-your-cluster-certificate).
- You have added your computer to the cluster IP allow list. Refer to [Assign IP Allow Lists](../../../cloud-secure-clusters/add-connections/).

## Clone the application from GitHub

Clone the sample application to your computer:

```sh
git clone https://github.com/yugabyte/yugabyte-simple-php-app && cd yugabyte-simple-php-app
```

## Provide connection parameters

The application needs to establish a connection to the YugabyteDB cluster. To do this:

1. Open the `sample-app.php` file.

1. Set the following configuration-related constants:

- **HOST** - the host name of your YugabyteDB cluster. To obtain a Yugabyte Cloud cluster host name, sign in to Yugabyte Cloud, select your cluster on the **Clusters** page, and click **Settings**. The host is displayed under **Network Access**.
- **PORT** - the port number for the driver to use; this is already set to the default YugabyteDB YSQL port (5433).
- **DB_NAME** - the name of the database to connect to (the default database is named `yugabyte`).
- **USER** and **PASSWORD** - the username and password for the YugabyteDB database. If you are using the credentials you created when deploying a cluster in Yugabyte Cloud, these can be found in the credentials file you downloaded.
- **SSL_MODE** - the SSL mode to use. Yugabyte Cloud [requires SSL connections](../../../cloud-secure-clusters/cloud-authentication/#ssl-modes-in-ysql); use `verify-full`.
- **SSL_ROOT_CERT** - the full path to the Yugabyte Cloud cluster CA certificate.

1. Save the file.

## Run the application

Run the application.

```sh
$ php sample-app.php
```

You should see output similar to the following:

```output
>>>> Successfully connected to YugabyteDB!
>>>> Successfully created table DemoAccount.
>>>> Selecting accounts:
name = Jessica, age = 28, country = USA, balance = 10000
name = John, age = 28, country = Canada, balance = 9000
>>>> Transferred 800 between accounts.
>>>> Selecting accounts:
name = Jessica, age = 28, country = USA, balance = 9200
name = John, age = 28, country = Canada, balance = 9800
```

You have successfully executed a basic PHP application that works with Yugabyte Cloud.

## Explore the application logic

Open the `sample-app.php` file in the `yugabyte-simple-php-app` folder to review the methods.

### connect

The `connect` method establishes a connection with your cluster via the php-pgsql driver.

```php
$conn = new PDO('pgsql:host=' . HOST . ';port=' . PORT . ';dbname=' . DB_NAME .
';sslmode=' . SSL_MODE . ';sslrootcert=' . SSL_ROOT_CERT,
USER, PASSWORD,
array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_EMULATE_PREPARES => true,
PDO::ATTR_PERSISTENT => true));
```

### create_database

The `create_database` method uses PostgreSQL-compliant DDL commands to create a sample database.

```php
$conn->exec('DROP TABLE IF EXISTS DemoAccount');

$conn->exec('CREATE TABLE DemoAccount (
id int PRIMARY KEY,
name varchar,
age int,
country varchar,
balance int)');

$conn->exec("INSERT INTO DemoAccount VALUES
(1, 'Jessica', 28, 'USA', 10000),
(2, 'John', 28, 'Canada', 9000)");
```

### select_accounts

The `select_accounts` method queries your distributed data using the SQL `SELECT` statement.

```php
$query = 'SELECT name, age, country, balance FROM DemoAccount';

foreach ($conn->query($query) as $row) {
print 'name=' . $row['name'] . ', age=' . $row['age'] . ', country=' . $row['country'] . ', balance=' . $row['balance'] . "\n";
}
```

### transfer_money_between_accounts

The `transfer_money_between_accounts` method updates your data consistently with distributed transactions.

```php
try {
$conn->beginTransaction();
$conn->exec("UPDATE DemoAccount SET balance = balance - " . $amount . " WHERE name = 'Jessica'");
$conn->exec("UPDATE DemoAccount SET balance = balance + " . $amount . " WHERE name = 'John'");
$conn->commit();
print ">>>> Transferred " . $amount . " between accounts\n";
} catch (PDOException $e) {
if ($e->getCode() == '40001') {
print "The operation is aborted due to a concurrent transaction that is modifying the same set of rows.
Consider adding retry logic for production-grade applications.\n";
}

throw $e;
}
```

## Learn more

[php-pgsql driver](../../../../reference/drivers/ysql-client-drivers/#php-pgsql)

[Explore more applications](../../../cloud-develop)

[Deploy clusters in Yugabyte Cloud](../../../cloud-basics)

[Connect to applications in Yugabyte Cloud](../../../cloud-connect/connect-applications/)
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ The following tutorial shows a small [Python application](https://github.com/yug

## Prerequisites

In addition to Python 3.6 or later (Python 3.9.7 or later if running macOS on Apple silicon), this tutorial requires the following.
- Python 3.6 or later (Python 3.9.7 or later if running macOS on Apple silicon).

### Yugabyte Cloud

Expand Down
28 changes: 15 additions & 13 deletions docs/content/latest/yugabyte-cloud/cloud-quickstart/qs-add.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,35 @@ showAsideToc: true

The free cluster provides a fully functioning single node YugabyteDB cluster deployed to the region of your choice. The cluster is free forever and includes enough resources to explore the core features available for developing applications with YugabyteDB. No credit card information is required. You can only have one free cluster.

To create a free cluster:
After setting up your Yugabyte Cloud account, [log in](https://cloud.yugabyte.com/login) to display the Yugabyte Cloud console. The console has the following main sections, accessed via the left menu:

- **Getting Started** - Create your free cluster and access documentation.

1. After setting up your Yugabyte Cloud account, [log in](https://cloud.yugabyte.com/login) to display the Yugabyte Cloud console. The console has the following main sections, accessed via the left menu:
- **Clusters** - Add, monitor, and manage clusters.

- **Getting Started** - Create your free cluster and access documentation.
- **Alerts** - Configure cluster and billing alerts, and view notifications.

- **Clusters** - Add, monitor, and manage clusters.
- **Network Access** - Set up VPC networks and authorize access to your clusters using IP allow lists.

- **Network Access** - Set up VPC networks and authorize access to your clusters using IP allow lists.
- **Admin** - Manage billing and payment methods, add cloud users, and review cloud activity.

- **Admin** - Manage billing and payment methods, add cloud users, and review cloud activity.
## Create a free cluster

To create a free cluster:

1. On the **Getting Started** page, click **Create a free cluster** to open the **Create Cluster** wizard.

1. Select **Yugabyte Cloud Free** and click **Next**.

1. Choose the provider (AWS or GCP), enter a name for the cluster, and choose the region, then click **Next**.

1. Choose the credentials you'll use to connect to your YugabyteDB database in the cloud. You can choose the default set with a database user named "admin", or create your own.
1. Choose the cloud provider (AWS or GCP), enter a name for the cluster, and choose the region in which to deploy the cluster, then click **Next**.

1. Click **Download credentials** and save your credentials in a secure location.
1. Click **Download credentials**. The default credentials are for a database user named "admin". You'll use these credentials when connecting to your YugabyteDB database in the cloud.

1. Verify that your credentials are downloaded, and click **Create Cluster**.
1. Click **Create Cluster**.

Once you complete the wizard, Yugabyte Cloud bootstraps and provisions the cluster, and configures YugabyteDB. The process takes up to 15 minutes.
After you complete the wizard, Yugabyte Cloud bootstraps and provisions the cluster, and configures YugabyteDB. The process takes up to 15 minutes.

Once the cluster is ready, the cluster [Overview](../../cloud-monitor/overview/) is displayed. You now have a fully configured YugabyteDB cluster provisioned in Yugabyte Cloud.
When the cluster is ready, the cluster [Overview](../../cloud-monitor/overview/) is displayed. You now have a fully configured YugabyteDB cluster provisioned in Yugabyte Cloud.

### Learn more

Expand Down
Loading

0 comments on commit 2b8e813

Please sign in to comment.