PHP实现对MongoDB的基础操作
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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

This article introduces how to configure MongoDB on Debian system to achieve automatic expansion. The main steps include setting up the MongoDB replica set and disk space monitoring. 1. MongoDB installation First, make sure that MongoDB is installed on the Debian system. Install using the following command: sudoaptupdatesudoaptinstall-ymongodb-org 2. Configuring MongoDB replica set MongoDB replica set ensures high availability and data redundancy, which is the basis for achieving automatic capacity expansion. Start MongoDB service: sudosystemctlstartmongodsudosys

In PHP, you can effectively prevent CSRF attacks by using unpredictable tokens. Specific methods include: 1. Generate and embed CSRF tokens in the form; 2. Verify the validity of the token when processing the request.

The... (splat) operator in PHP is used to unpack function parameters and arrays, improving code simplicity and efficiency. 1) Function parameter unpacking: Pass the array element as a parameter to the function. 2) Array unpacking: Unpack an array into another array or as a function parameter.

In PHP, the final keyword is used to prevent classes from being inherited and methods being overwritten. 1) When marking the class as final, the class cannot be inherited. 2) When marking the method as final, the method cannot be rewritten by the subclass. Using final keywords ensures the stability and security of your code.
