search

PHP扩展

Jun 21, 2016 am 08:48 AM
collectionechogtnbspquot

下载相对应的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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment