diff --git a/source/get-started.txt b/source/get-started.txt index ac7e9788..d9a61995 100644 --- a/source/get-started.txt +++ b/source/get-started.txt @@ -23,6 +23,7 @@ Get Started with the PHP Library /get-started/download-and-install/ /get-started/create-a-deployment/ /get-started/create-a-connection-string/ + /get-started/connect-to-mongodb/ /get-started/next-steps/ Overview diff --git a/source/get-started/connect-to-mongodb.txt b/source/get-started/connect-to-mongodb.txt new file mode 100644 index 00000000..7492fe25 --- /dev/null +++ b/source/get-started/connect-to-mongodb.txt @@ -0,0 +1,69 @@ +.. _php-connect-to-mongodb: + +================== +Connect to MongoDB +================== + +.. facet:: + :name: genre + :values: tutorial + +.. meta:: + :keywords: test connection, runnable, code example + +After retrieving the connection string for your MongoDB Atlas deployment, +you can connect to the deployment from your PHP application and query +the Atlas sample datasets. + +.. procedure:: + :style: connected + + .. step:: Edit your PHP application file + + Copy and paste the following code into the ``quickstart.php`` file, which queries + the ``movies`` collection in the ``sample_mflix`` database: + + .. literalinclude:: /includes/get-started/quickstart.php + :language: php + :dedent: + + .. step:: Assign the connection string + + Replace the ```` placeholder with the + connection string that you copied from the :ref:`php-connection-string` + step of this guide. + + .. step:: Run your PHP application + + In your project directory, run the following shell command to start the application: + + .. code-block:: bash + + php quickstart.php + + The command line output contains details about the retrieved movie + document: + + .. code-block:: none + :copyable: false + + { + "_id": { + "$oid": "..." + }, + ... + "rated": "R", + "metacritic": 80, + "title": "The Shawshank Redemption", + ... + } + + If you encounter an error or see no output, ensure that you specified the + proper connection string in the ``quickstart.php`` file and that you loaded the + sample data. + +After you complete these steps, you have a PHP application that +connects to your MongoDB deployment, runs a query on the sample +data, and returns a matching document. + +.. include:: /includes/get-started/troubleshoot.rst diff --git a/source/includes/get-started/quickstart.php b/source/includes/get-started/quickstart.php new file mode 100644 index 00000000..d62dd83e --- /dev/null +++ b/source/includes/get-started/quickstart.php @@ -0,0 +1,17 @@ +'); +$collection = $client->sample_mflix->movies; + +$filter = ['title' => 'The Shawshank Redemption']; +$result = $collection->findOne($filter); + +if ($result) { + echo json_encode($result, JSON_PRETTY_PRINT); +} else { + echo "Document not found"; +}