Learn from your fellow PHP developers with our PHP blogs, or help share the knowledge you've gained by writing your own.
composer require mongodb/mongodb
vendor
directory containing the required files.connect.php
. Add the following code:
<?php
require 'vendor/autoload.php';
use MongoDB\Client;
$client = new Client("mongodb://localhost:27017");
?>
Client
class. We then create a new instance of the Client
class, specifying the MongoDB server's connection URL. Adjust the URL if your MongoDB server is running on a different host or port.insertOne()
method. Here's an example:<?php
$collection = $client->test->users;
$newUser = [
'name' => 'John',
'email' => '[email protected]',
'age' => 25
];
$insertResult = $collection->insertOne($newUser);
echo "Inserted document ID: " . $insertResult->getInsertedId();
?>
users
collection within the test
database. We create a new document as an associative array and then use the insertOne()
method to insert it into the collection. Finally, we retrieve and display the ID of the inserted document using the getInsertedId()
method.find()
method. Here's an example:<?php
$collection = $client->test->users;
$documents = $collection->find();
foreach ($documents as $document) {
echo $document['name'] . ': ' . $document['email'] . "\n";
}
?>
users
collection. We iterate over the result using a foreach
loop and access specific fields, such as the name
and email
, to display their values.updateOne()
method. Here's an example:<?php
$collection = $client->test->users;
$updateResult = $collection->updateOne(
['name' => 'John'],
['$set' => ['age' => 30]]
);
echo "Modified " . $updateResult->getModifiedCount() . " document(s).";
?>
age
field of the document with the name 'John' using the $set
operator. The updateOne()
method updates the first matching document. We then retrieve the number of modified documents using the getModifiedCount()
method.deleteOne()
method. Here's an example:<?php
$collection = $client->test->users;
$deleteResult = $collection->deleteOne(['name' => 'John']);
echo "Deleted " . $deleteResult->getDeletedCount() . " document(s).";
?>
deleteOne()
method removes the first matching document, and we retrieve the number of deleted documents using the getDeletedCount()
method..php
. This tells the server to treat this page as a PHP script.
<html>
<head>
<title>Hello</title>
</head>
<body>
Hello
</body>
</html>
<?php
tag, then we write our PHP code, and finally close it with the ?>
tag. This is important as if we were creating an XML file and forgot to escape the opening XML tag which also has a question mark, we would run into a fatal error.echo
statement in our PHP code by placing the text we want to echo in single quotes and then end the command with a semi colon. Let's echo out "there!":
<html>
<head>
<title>Hello</title>
</head>
<body>
Hello <?php echo 'there!'; ?>
</body>
</html>
$_GET
?name=yourname
name
in the querystring, we can use $_GET['name']
like so:
<html>
<head>
<title>Hello</title>
</head>
<body>
Hello <?php echo $_GET['name']; ?>
</body>
</html>
$_GET['name']
."if
, elseif
, and else
along with the PHP date()
function. You can learn more about how to use different date formats to output the date here, but we'll be using the date()
function to get back the hour of the day (based on the server's time) between 0 (midnight) and 23 (11pm). We'll then use greater than (>) to determine what to assign to our $time
variable which we'll output with the user's name.
<html>
<head>
<title>Hello</title>
</head>
<body>
<?php
if(date("G") > 18) {
$time = 'evening';
} elseif (date("G") > 12) {
$time = 'afternoon';
} else {
$time = 'morning';
}
echo 'Good '.$time.' '.$_GET['name'];
?>
</body>
</html>
<?php
):
date_default_timezone_set('America/Los_Angeles');