PHP扩展 PHP5.2、PHP5.3,线程、非线程,IIS、Apache综合下载地址: http://pan.baidu.com/s/1sjrW6z7 下载相对应的php扩展文件php_mongo.dll,拷入php扩展文件夹里/php/ext,修改php.ini文件,新增下列行: extension=php_mongo.dll PHP 操作 连接数据库 $c
PHP扩展
PHP5.2、PHP5.3,线程、非线程,IIS、Apache综合下载地址:
http://pan.baidu.com/s/1sjrW6z7
下载相对应的php扩展文件php_mongo.dll,拷入php扩展文件夹里/php/ext,修改php.ini文件,新增下列行:
extension=php_mongo.dll
PHP操作
<span>$conn</span> = <span>new</span> Mongo("mongodb://localhost:27017//admin:admin");
<span>//</span><span>选择数据库blog,如果没有,则创建</span> <span>$db</span> = <span>$conn</span>-><span>yyd; </span><span>//</span><span>也可以写成:$db = $conn->selectDB('yyd'); //制定结果集(集合:yyd_test)</span> <span>$collection</span> = <span>$db</span>-><span>yyd_test; </span><span>//</span><span>也可以写成:$collection = $db->selectCollection('yyd_test'); //var_dump($collection);</span>
<span>$post</span> = <span>array</span>('name' => '22', 'sex' => '32'<span>); </span><span>$flag</span>=(<span>$collection</span>->insert(<span>$post</span><span>)); </span><span>var_dump</span>(<span>$flag</span>);
<span>$arr</span>=<span>array</span><span>(); </span><span>$cursor</span> = <span>$collection</span>->find(<span>$arr</span><span>); </span><span>foreach</span>(<span>$cursor</span> <span>as</span> <span>$key</span> => <span>$value</span><span>){ </span><span>echo</span> "<pre class="brush:php;toolbar:false">"<span>;</span> <span>echo</span> <span>$value</span>['_id'<span>]; </span><span>echo</span> '<br>name:'<span>; </span><span>echo</span> <span>$value</span>['name'<span>]; </span><span>echo</span> "<br>sex:"<span>; </span><span>echo</span> <span>$value</span>['sex'<span>]; </span><span>echo</span> "
<span>$arr</span>=<span>array</span>("name"=>"22"<span>); </span><span>$cursor</span> = <span>$collection</span>->find(<span>$arr</span>);
<span>$newdata</span> = <span>array</span>('$set' => <span>array</span>("email" => "test@test.com"<span>)); </span><span>$collection</span>->update(<span>array</span>("name" => "22"), <span>$newdata</span><span>); </span><span>var_dump</span>(<span>$collection</span><span>); </span><span>$arr</span>=<span>array</span>("name"=>"22"<span>); </span><span>$cursor</span> = <span>$collection</span>->find(<span>$arr</span><span>); </span><span>foreach</span>(<span>$cursor</span> <span>as</span> <span>$key</span> => <span>$value</span><span>){ </span><span>echo</span> "<pre class="brush:php;toolbar:false">"<span>;</span>
<span>echo</span> <span>$value</span>['_id'<span>]; </span><span>echo</span> '<br>name:'<span>; </span><span>echo</span> <span>$value</span>['name'<span>]; </span><span>echo</span> "<br>sex:"<span>; </span><span>echo</span> <span>$value</span>['sex'<span>]; </span><span>echo</span> "<br>email:"<span>; </span><span>echo</span> <span>$value</span>['email'<span>]; </span><span>echo</span> "
<span>$arr</span>=<span>array</span>("name"=>"22"<span>); </span><span>$collection</span>->remove(<span>$arr</span><span>); </span><span>var_dump</span>(<span>$collection</span>);
<span>//</span><span>关闭连接</span> <span>$conn</span>-><span>close(); </span><span>//</span><span>删除一个数据库</span> <span>$conn</span>->dropDB("yyd"<span>); </span><span>//</span><span>列出所有可用数据库</span> <span>$dbs</span> = <span>$conn</span>->listDBs();
带有MongoDB操作函数的的PHP手册:
http://pan.baidu.com/s/1pJz2llh
或者网站:
http://www.php.net/manual/zh/class.mongodb.php
http://www.cnblogs.com/yydcdut/p/3571430.html