PHP连接示例
<p>本文主要介绍PHP连接示例。</p>
<p> <img src="https://pcp-portal-sca.obs-cn-shenzhen.pinganyun.com/pcp-portal-sca/20202805101934-1f875b7c9f9d.png" style="height:26px; margin:0px; width:65px" />:官方资料可参考<a href="https://docs.mongodb.com/drivers/php" target="_blank">Mongodb php driver</a>。</p>
<p><span style="font-size:18px"><strong>操作步骤</strong></span></p>
<p>1. 执行以下命令,安装驱动包以及工具包。</p>
<table border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="background-color:#f7f8fa">
<p><span style="font-family:Consolas">$ pecl install mongodb</span></p>
<p><span style="font-family:Consolas">$ echo "extension=mongodb.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`</span></p>
<p><span style="font-family:Consolas">$ composer <strong>require</strong> "mongodb/mongodb=^1.0.0"</span></p>
</td>
</tr>
</tbody>
</table>
<p>2. 获取DDS实例的连接信息。</p>
<p>3. PHP连接代码:</p>
<table border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="background-color:#f7f8fa">
<p><span style="font-family:Consolas"><?php</span></p>
<p><span style="font-family:Consolas"><strong>require</strong> 'vendor/autoload.php'; // include Composer goodies</span></p>
<p><span style="font-family:Consolas"># 实例信息</span></p>
<p><span style="font-family:Consolas">$demo_seed = 'demotest.mongodb.db.cloud.papub:15923';</span></p>
<p><span style="font-family:Consolas">$demo_replname = "demotest";</span></p>
<p><span style="font-family:Consolas">$demo_user = 'demouser';</span></p>
<p><span style="font-family:Consolas">$demo_password = '123456';</span></p>
<p><span style="font-family:Consolas">$demo_db = 'test';</span></p>
<p><span style="font-family:Consolas"># 根据实例信息构造mongodb connection string</span></p>
<p><span style="font-family:Consolas">#mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]</span></p>
<p><span style="font-family:Consolas">$demo_uri = 'mongodb://' . $demo_user . ':' . $demo_password . '@' .</span></p>
<p><span style="font-family:Consolas"> $demo_seed . '/' . $demo_db . '?replicaSet=' . $demo_replname;</span></p>
<p><span style="font-family:Consolas">$client = <strong>new</strong> MongoDB\Client($demo_uri);</span></p>
<p><span style="font-family:Consolas">$collection = $client->testDb->testColl;</span></p>
<p><span style="font-family:Consolas">$result = $collection->insertOne(['name' => 'ApsaraDB for Mongodb', 'desc' => 'Hello, Mongodb']);</span></p>
<p><span style="font-family:Consolas"><strong>echo</strong> "Inserted with Object ID '{$result->getInsertedId()}'", "\n";</span></p>
<p><span style="font-family:Consolas">$result = $collection->find(['name' => 'ApsaraDB for Mongodb']);</span></p>
<p><span style="font-family:Consolas"><strong>foreach</strong> ($result <strong>as</strong> $entry) {</span></p>
<p><span style="font-family:Consolas"> <strong>echo</strong> $entry->_id, ': ', $entry->name, "\n";</span></p>
<p><span style="font-family:Consolas">}</span></p>
<p><span style="font-family:Consolas">?></span></p>
</td>
</tr>
</tbody>
</table>
提交成功!非常感谢您的反馈,我们会继续努力做到更好!