Home  >  Article  >  php教程  >  PHP扩展

PHP扩展

WBOY
WBOYOriginal
2016-06-21 08:48:331042browse

下载相对应的php扩展文件php_mongo.dll,拷入php扩展文件夹里/php/ext,修改php.ini文件,新增下列行:

 

extension=php_mongo.dll

 

PHP操作                                                                                     

 

连接数据库

$conn = new Mongo("mongodb://localhost:27017//admin:admin");

选择数据库和集合

复制代码

//选择数据库blog,如果没有,则创建

 

$db = $conn->yyd;

 

//也可以写成:$db = $conn->selectDB('yyd');

 

 

//制定结果集(集合:yyd_test)

$collection = $db->yyd_test;

 

//也可以写成:$collection = $db->selectCollection('yyd_test');

//var_dump($collection);

复制代码

新增数据

$post = array('name' => '22', 'sex' => '32');

 

$flag=($collection->insert($post));

var_dump($flag);

image

 

image

 

查找数据

复制代码

$arr=array();

$cursor = $collection->find($arr);

foreach($cursor as $key => $value){

    echo "

";
<p>    echo $value['_id'];</p>
<p>    echo '<br>name:';</p>
<p>    echo $value['name'];</p>
<p>    echo "<br>sex:";</p>
<p>    echo $value['sex'];</p>
<p>    echo "</p>
";

}

复制代码

image

 

条件查找

$arr=array("name"=>"22");

$cursor = $collection->find($arr);

修改数据

image

 

复制代码

$newdata = array('$set' => array("email" => "test@test.com"));

$collection->update(array("name" => "22"), $newdata);

var_dump($collection);

 

$arr=array("name"=>"22");

$cursor = $collection->find($arr);

foreach($cursor as $key => $value){

    echo "

";
<p>    echo $value['_id'];</p>
<p>    echo '<br>name:';</p>
<p>    echo $value['name'];</p>
<p>    echo "<br>sex:";</p>
<p>    echo $value['sex'];</p>
<p>    echo "<br>email:";</p>
<p>    echo $value['email'];</p>
<p>    echo "</p>
";

}

复制代码

删除数据

image

 

$arr=array("name"=>"22");

 

$collection->remove($arr);

 

var_dump($collection);

image

 

其它常用操作

复制代码

//关闭连接

 

$conn->close();

 

 

//删除一个数据库

 

$conn->dropDB("yyd");

 

 

//列出所有可用数据库

 

$dbs = $conn->listDBs();



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn