PHP连接示例

<p>本文主要介绍PHP连接示例。</p> <p>&nbsp; &nbsp;&nbsp;<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.&nbsp; 执行以下命令,安装驱动包以及工具包。</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 &quot;extension=mongodb.so&quot; &gt;&gt; `php --ini | grep &quot;Loaded Configuration&quot; | sed -e &quot;s|.*:\s*||&quot;`</span></p> <p><span style="font-family:Consolas">$ composer <strong>require</strong> &quot;mongodb/mongodb=^1.0.0&quot;</span></p> </td> </tr> </tbody> </table> <p>2.&nbsp; 获取DDS实例的连接信息。</p> <p>3.&nbsp; PHP连接代码:</p> <table border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td style="background-color:#f7f8fa"> <p><span style="font-family:Consolas">&lt;?php</span></p> <p><span style="font-family:Consolas"><strong>require</strong> &#39;vendor/autoload.php&#39;; // include Composer goodies</span></p> <p><span style="font-family:Consolas"># 实例信息</span></p> <p><span style="font-family:Consolas">$demo_seed = &#39;demotest.mongodb.db.cloud.papub:15923&#39;;</span></p> <p><span style="font-family:Consolas">$demo_replname = &quot;demotest&quot;;</span></p> <p><span style="font-family:Consolas">$demo_user = &#39;demouser&#39;;</span></p> <p><span style="font-family:Consolas">$demo_password = &#39;123456&#39;;</span></p> <p><span style="font-family:Consolas">$demo_db = &#39;test&#39;;</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 = &#39;mongodb://&#39; . $demo_user . &#39;:&#39; . $demo_password . &#39;@&#39; .</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp; $demo_seed . &#39;/&#39; . $demo_db . &#39;?replicaSet=&#39; . $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-&gt;testDb-&gt;testColl;</span></p> <p><span style="font-family:Consolas">$result = $collection-&gt;insertOne([&#39;name&#39; =&gt; &#39;ApsaraDB for Mongodb&#39;, &#39;desc&#39; =&gt; &#39;Hello, Mongodb&#39;]);</span></p> <p><span style="font-family:Consolas"><strong>echo</strong> &quot;Inserted with Object ID &#39;{$result-&gt;getInsertedId()}&#39;&quot;, &quot;\n&quot;;</span></p> <p><span style="font-family:Consolas">$result = $collection-&gt;find([&#39;name&#39; =&gt; &#39;ApsaraDB for Mongodb&#39;]);</span></p> <p><span style="font-family:Consolas"><strong>foreach</strong> ($result <strong>as</strong> $entry) {</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp; <strong>echo</strong> $entry-&gt;_id, &#39;: &#39;, $entry-&gt;name, &quot;\n&quot;;</span></p> <p><span style="font-family:Consolas">}</span></p> <p><span style="font-family:Consolas">?&gt;</span></p> </td> </tr> </tbody> </table>
以上内容是否解决了您的问题?
请补全提交信息!
咨询·建议

电话咨询

400-151-8800

邮件咨询

cloud@pingan.com

在线客服

工单支持

解决云产品相关技术问题