# configs详解——之db

**本节介绍db类用法**

### 数据库配置链接 <a href="#shu-ju-ku-pei-zhi-lian-jie" id="shu-ju-ku-pei-zhi-lian-jie"></a>

```
$db_config = array(
    'host'  => '127.0.0.1',
    'port'  => 3306,
    'user'  => 'root',
    'pass'  => 'root',
    'name'  => 'qiushibaike',
);
// 数据库配置
db::set_connect('default', $db_config);
// 数据库链接
db::init_mysql();
```

### 原生SQL操作 <a href="#yuan-sheng-sql-cao-zuo" id="yuan-sheng-sql-cao-zuo"></a>

#### `query($sql)` <a href="#querysql" id="querysql"></a>

举个栗子:

```
// 查询
$rsid = db::query("Select * From `content`");
while ( $row = db::fetch($rsid) )
{
    echo "id = {$row['id']}; name = {$row['name']}\n";
}

// 新增
db::query("Insert Into `content`(`name`) Value('test'));

// 更新
db::query("Update `content` Set `name`='test' Where `id`=1");

// 删除
db::query("Delete From `content` Where `id`='1'");
```

### CRUD操作 <a href="#crud-cao-zuo" id="crud-cao-zuo"></a>

#### `get_one($sql)` <a href="#getonesql" id="getonesql"></a>

**单条查询**

举个栗子:

```
$row = db::get_one("Select * From `content` Where `id`='1'");
```

#### `get_all($sql)` <a href="#getallsql" id="getallsql"></a>

**多条查询**

举个栗子:

```
$rows = db::get_all("Select * From `content` Limit 5");
```

#### `insert($table, $data)` <a href="#inserttable-data" id="inserttable-data"></a>

**单条插入**

举个栗子:

```
$data = array(
    'name' => 'test',
    'url'  => 'http://www.baidu.com'
);
$rows = db::insert('content', $data);
```

#### `insert_batch($table, $data)` <a href="#insertbatchtable-data" id="insertbatchtable-data"></a>

**单条修改**

举个栗子:

```
$data = array(
    array(
        'name' => 'test111',
        'url'  => 'http://www.baidu.com'
    ),
    array(
        'name' => 'test222',
        'url'  => 'http://www.baidu.com'
    ),
);
$rows = db::insert_batch('content', $data);
```

#### `update_batch($table, $data, $index)` <a href="#updatebatchtable-data-index" id="updatebatchtable-data-index"></a>

**批量修改**

举个栗子:

```
$data = array(
    array(
        'name' => 'test111',
        'url'  => 'http://www.baidu.com'
    ),
    array(
        'name' => 'test222',
        'url'  => 'http://www.baidu.com'
    ),
);
// 以name为条件进行修改
$rows = db::update_batch('content', $data, "name");
```

#### `delete($table, $where)` <a href="#deletetable-where" id="deletetable-where"></a>

**单条删除**

举个栗子:

```
$rows = db::delete('content', "`id`='1'");
```

### results matching ""

*

### No results matching ""


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://zhai-shi-sansorganization.gitbook.io/phpspider/configs-xiang-jie-zhi-db.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
