-
Notifications
You must be signed in to change notification settings - Fork 27
/
4.html
647 lines (647 loc) · 89.8 KB
/
4.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<title>第四章 使用 CI 简化数据库开发</title>
<link href="css/main.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/main.js"></script>
</head>
<body>
<div id="container"><a name="top"></a>
<div id="header">
<div class="title"><a href="3.html">上一页</a> | <a href="5.html">下一页</a> | <a href="table_of_contents.html">目录</a> | <a href="#bottom">转到页尾</a></div>
</div>
<div id="content">
<div class="main">
<h1>第四章 使用 CI 简化数据库开发</h1>
<p>你学习CI是因为你想要使编程更容易和更有生产力。这一章讲述CI的<strong>Active Record</strong>类。 如果CI只提供一个Active Record类,它还是物超所值的。当然,CI是免费的,只不过我要强调Active Record(以下简称AR)类的价值是非常高的,它是你提高生产力的主要工具。</p>
<p>AR使你以最小的代价获得最大的回报。它简单,易于使用和维护。</p>
<p>这一章描述CI如何连接到一个数据库,你如何使用AR操纵数据库。你将会见到:</p>
<ul>
<li>AR类与“经典”PHP/MySQL接口的比较</li>
<li>如何编写“读”查询并显示结果</li>
<li>如何编写创建、更新和删除的查询</li>
</ul>
<p>CI保留让你用传统的方法编写数据库查询,但是我不会详细介绍这部分内容。它的知识完全被在线手册覆盖。使用AR类后,你可能不会再用传统的方式来做数据库查询了。</p>
<a name="C_4_1"></a>
<h2>4.1 配置设置</h2>
<p>你或许已经注意到在这本书的大多数的章节会谈到system/application/config 文件夹和里面的config文件。这些文件对控制CI按要求工作相当必要。而且你可以让大部分的配置参数等于系统的默认值。数据库config文件在正常使用数据库之前需要进行设置。</p>
<p>基本上,你仅仅必须告诉它你的数据库在哪里、它是什么类型。文件的默认值为:</p>
<div class="code">
<div class="title">
<div style="float:right"><img class="copyCodeImage" src="images/copycode.gif" alt="" /><a href="javascript:void(0)" onclick="CopyCode(this)">复制代码到剪贴板</a></div>
<div style="clear:none">PHP 代码</div>
</div>
<pre class="php"><span style="color: #0000ff;">$active_group</span> = <span style="color: #ff0000;">"default"</span>;
<span style="color: #0000ff;">$db</span><span style="color: #66cc66;">[</span><span style="color: #ff0000;">'default'</span><span style="color: #66cc66;">]</span><span style="color: #66cc66;">[</span><span style="color: #ff0000;">'hostname'</span><span style="color: #66cc66;">]</span> = <span style="color: #ff0000;">""</span>;
<span style="color: #0000ff;">$db</span><span style="color: #66cc66;">[</span><span style="color: #ff0000;">'default'</span><span style="color: #66cc66;">]</span><span style="color: #66cc66;">[</span><span style="color: #ff0000;">'username'</span><span style="color: #66cc66;">]</span> = <span style="color: #ff0000;">""</span>;
<span style="color: #0000ff;">$db</span><span style="color: #66cc66;">[</span><span style="color: #ff0000;">'default'</span><span style="color: #66cc66;">]</span><span style="color: #66cc66;">[</span><span style="color: #ff0000;">'password'</span><span style="color: #66cc66;">]</span> = <span style="color: #ff0000;">""</span>;
<span style="color: #0000ff;">$db</span><span style="color: #66cc66;">[</span><span style="color: #ff0000;">'default'</span><span style="color: #66cc66;">]</span><span style="color: #66cc66;">[</span><span style="color: #ff0000;">'database'</span><span style="color: #66cc66;">]</span> = <span style="color: #ff0000;">""</span>;
<span style="color: #0000ff;">$db</span><span style="color: #66cc66;">[</span><span style="color: #ff0000;">'default'</span><span style="color: #66cc66;">]</span><span style="color: #66cc66;">[</span><span style="color: #ff0000;">'dbdriver'</span><span style="color: #66cc66;">]</span> = <span style="color: #ff0000;">""</span>;</pre>
</div>
<a name="I_database"></a>
<a name="I_dbdriver"></a>
<a name="I_hostname"></a>
<a name="I_password"></a>
<a name="I_username"></a>
<p>其他的选项可以保留为默认值。必选项是:</p>
<ul>
<li>hostname: 你的数据库的位置, 举例来说, 'localhost' 或 IP 地址</li>
<li>username和password: 使用者名称和密码必须有充分的权限,允许你的网站存取数据库中的数据。</li>
<li>database: 你的数据库的名字, 举例来说, 'websits'</li>
<li>dbdriver: 你正在使用的数据库的类型 - CI可受的有选项有MySQL、MySQLi、 Postgre SQL、ODBC和MS SQL</li>
</ul>
<p>以我的经验来看, 最困难的事情之一就是把新的CI网站连接到数据库。你可能需要查询你的ISP。有时他们的数据库运行在与他们的web server IP地址不同的地方。如果你正在使用 MySQL,他们可能提供 phpMyAdmin,通常告诉你hostname-这可能是 'localhost' 或者它可能是一个 IP 地址。</p>
<p>你可能注意到 config 文件的内容实际上是一个多维数组。在 $db数组里包含一个叫做default的数组,你所做的设置就是往里增加键/值对,例如 hostname = 127.0.0.1 。你还可以增加其他的数据库设置, 通过改变$active_group的设置可以容易地更改数据据库。</p>
<p>这为网站连接到数个数据库提供了可能性-举例来说, 一个测试数据库和一个产品数据库。你可以很容易地在他们之间切换。或者你可以在两个数据库之间交换数据。</p>
<a name="C_4_2"></a>
<h2>4.2 为我们的网站设计数据库</h2>
<p>我想表达的是 CI 能用来开发正式的网站。 我现在正在维护客户的一些网站,而且我想要监控他们,用我设计的方法测试它们, 用数据库保存我想要的数据, 而且可以得到这些网站的分析报告。 因此让我们试着创建它。 先让我们确定一些目标。 它们是:</p>
<ol>
<li>用最少的人工干预管理一个或更多的远程网站</li>
<li>对远程网站进行定期的测试</li>
<li>生成符合要求的分析报告, 提供网站的细节和测试结果</li>
</ol>
<p>因此, 第一件事情是我们将会需要一个网站的数据库。 建立一个名为websites的MySQL数据库,你也可以使用别的数据库产品。</p>
<p>现在,我们需要增加一些表来保存各种数据。让我们为网站增加一张表,字段有URL,他们的名字和密码/用户名, 和他们的类型。 我们也将会为每个网站建立一个ID字段。而且在 MySQL数据库中,至少需要为实体生成一个唯一标识符,可以使用自动增量类型来达到这一目的。</p>
<p>每个网站必须有一个不同的主机,我们需要另一表来保存主机信息。一般有一个域名与主机相关联,所以我们需要一个域名表来保存有关域名的信息,还需要一个人员表来记录这些人的姓名,密码,邮件地址,备用邮件地址,手机号码,曾至宠物的名字,可能还有其它的一些什么。</p>
<p>因此我们的网站表需要包括这样一些字段:domain ID, host ID, 两个people ID,一个存放网站站长的ID一个存放网站管理人的ID(管理人为网站提供技术支持,保证网站正常运行。)</p>
<p>你能见到,这是一个完整的关系型数据库,让我们来建立它!(本章的附录中有该数据库的详细资料,如果你想创建这个数据库,请执行该 MySQL 查询。)</p>
<p>我们现在要用一个更简单容易的方法来实现这一切。 所以,让我们看看CI框架为我们提供了什么功能,我们要重点介绍AR类。</p>
<a name="C_4_3"></a>
<h2>4.3 Active Record</h2>
<p>AR是一个“设计模式”。另一方面又是一个高度抽象的东西,就像 MVC。 它本身不是代码, 只是一个模式。对于代码,有一些不同的解释。它的核心是把你的数据库和PHP对象建立一个对应关系。每次当你执行一个QUERY语句。每一张数据库里的表是一个类,每一行是一个对象。所有你需要做的只是创建它,修改它或者删除它。例如,“方法”,是从类继承而来。Ruby on Rails是使用AR模式的,CI也是,尽管这两种框架实现AR的方式有一点不同。</p>
<p>理论的东西够多了-但是这是什么意思呢?好吧, 用代码简单和清楚地描述一下吧。</p>
<a name="C_4_3_1"></a>
<h2>4.3.1 使用 Active Record 类的优点</h2>
<p>AR 节省你的时间,自动运作,使 SQL 语句方便易懂。</p>
<a name="C_4_3_1_1"></a>
<h2>4.3.1.1 节省时间</h2>
<p>当你在编写 PHP 程序时,每写一个数据库查询,你就要与数据库建立连接。对 CI 来说,第一次连接数据库时,你在每个控制器或模型的构造函数里放入这样一行代码:</p>
<div class="code">
<div class="title">
<div style="float:right"><img class="copyCodeImage" src="images/copycode.gif" alt="" /><a href="javascript:void(0)" onclick="CopyCode(this)">复制代码到剪贴板</a></div>
<div style="clear:none">PHP 代码</div>
</div>
<pre class="php"><span style="color: #0000ff;">$this</span>-><span style="color: #006600;">load</span>-><span style="color: #006600;">database</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;</pre>
</div>
<p>一旦你这样做了,你不需要重复连接, 在那个控制器或模型就可以做任意多次的查询。</p>
<p>你已经在 config文件中设置了关于数据库的参数,就像我们在这一章开始时看到的一样。再一次,这使更新你的网站比较容易,如果你想要改变数据库名字、密码或位置的话。</p>
<a name="C_4_3_1_2"></a>
<h2>4.3.1.2 自动机制</h2>
<p>你一旦已经连接到数据库,CI的AR生成隐含的代码。举例来说,如果你进行下列的插入操作:</p>
<div class="code">
<div class="title">
<div style="float:right"><img class="copyCodeImage" src="images/copycode.gif" alt="" /><a href="javascript:void(0)" onclick="CopyCode(this)">复制代码到剪贴板</a></div>
<div style="clear:none">PHP 代码</div>
</div>
<pre class="php"><span style="color: #0000ff;">$data</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span>
<span style="color: #ff0000;">'title'</span> => <span style="color: #0000ff;">$title</span>,
<span style="color: #ff0000;">'name'</span> => <span style="color: #0000ff;">$name</span>,
<span style="color: #ff0000;">'date'</span> => <span style="color: #0000ff;">$date</span>
<span style="color: #66cc66;">)</span>;
<span style="color: #0000ff;">$this</span>-><span style="color: #006600;">db</span>-><span style="color: #006600;">insert</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'mytable'</span>, <span style="color: #0000ff;">$data</span><span style="color: #66cc66;">)</span>;</pre>
</div>
<p>你正在插入的数据($data)已经在幕后被转换成这样一段代码:</p>
<div class="code">
<div class="title">
<div style="float:right"><img class="copyCodeImage" src="images/copycode.gif" alt="" /><a href="javascript:void(0)" onclick="CopyCode(this)">复制代码到剪贴板</a></div>
<div style="clear:none">PHP 代码</div>
</div>
<pre class="php"><span style="color: #000000; font-weight: bold;">function</span> escape<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$str</span><span style="color: #66cc66;">)</span>
<span style="color: #66cc66;">{</span>
<span style="color: #b1b100;">switch</span> <span style="color: #66cc66;">(</span><a href="http://www.php.net/gettype"><span style="color: #000066;">gettype</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$str</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>
<span style="color: #66cc66;">{</span>
<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">'string'</span>:
<span style="color: #0000ff;">$str</span> = <span style="color: #ff0000;">"'"</span>.<span style="color: #0000ff;">$this</span>-><span style="color: #006600;">escape_str</span><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$str</span><span style="color: #66cc66;">)</span>.<span style="color: #ff0000;">"'"</span>;
<span style="color: #b1b100;">break</span>;
<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">'boolean'</span>: <span style="color: #0000ff;">$str</span> = <span style="color: #66cc66;">(</span><span style="color: #0000ff;">$str</span> === <span style="color: #000000; font-weight: bold;">FALSE</span><span style="color: #66cc66;">)</span> ? <span style="color: #cc66cc;">0</span> : <span style="color: #cc66cc;">1</span>;
<span style="color: #b1b100;">break</span>;
<span style="color: #000000; font-weight: bold;">default</span> : <span style="color: #0000ff;">$str</span> = <span style="color: #66cc66;">(</span><span style="color: #0000ff;">$str</span> === <span style="color: #000000; font-weight: bold;">NULL</span><span style="color: #66cc66;">)</span> ? <span style="color: #ff0000;">'NULL'</span> : <span style="color: #0000ff;">$str</span>;
<span style="color: #b1b100;">break</span>;
<span style="color: #66cc66;">}</span>
<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$str</span>;
<span style="color: #66cc66;">}</span></pre>
</div>
<p>换句话说, CI框架使你的代码变得更强健。 现在,让我们看看它是如何工作的。</p>
<p>第一, 连接数据库非常简单。在传统的PHP编程时,你可能是这样做的:</p>
<div class="code">
<div class="title">
<div style="float:right"><img class="copyCodeImage" src="images/copycode.gif" alt="" /><a href="javascript:void(0)" onclick="CopyCode(this)">复制代码到剪贴板</a></div>
<div style="clear:none">PHP 代码</div>
</div>
<pre class="php"><span style="color: #0000ff;">$connection</span> = <a href="http://www.php.net/mysql_connect"><span style="color: #000066;">mysql_connect</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"localhost"</span>,<span style="color: #ff0000;">"fred"</span>,<span style="color: #ff0000;">"12345"</span><span style="color: #66cc66;">)</span>;
<a href="http://www.php.net/mysql_select_db"><span style="color: #000066;">mysql_select_db</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"websites"</span>, <span style="color: #0000ff;">$connection</span><span style="color: #66cc66;">)</span>;
<span style="color: #0000ff;">$result</span> = <a href="http://www.php.net/mysql_query"><span style="color: #000066;">mysql_query</span></a> <span style="color: #66cc66;">(</span><span style="color: #ff0000;">"SELECT * FROM sites"</span>, <span style="color: #0000ff;">$connection</span><span style="color: #66cc66;">)</span>;
<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">(</span><span style="color: #0000ff;">$row</span> = <a href="http://www.php.net/mysql_fetch_array"><span style="color: #000066;">mysql_fetch_array</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$result</span>, MYSQL_NUM<span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>
<span style="color: #66cc66;">{</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">(</span><span style="color: #0000ff;">$row</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$attribute</span><span style="color: #66cc66;">)</span>
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">"{$attribute[1]} "</span>;
<span style="color: #66cc66;">}</span></pre>
</div>
<p>换句话说,你必须重复地输入host、username和password,建立一个连接,然后选择一个连接的数据库。你必须每次都这样做,然后再开始一个查询。CI以一条命令替换连接工作:</p>
<div class="code">
<div class="title">
<div style="float:right"><img class="copyCodeImage" src="images/copycode.gif" alt="" /><a href="javascript:void(0)" onclick="CopyCode(this)">复制代码到剪贴板</a></div>
<div style="clear:none">PHP 代码</div>
</div>
<pre class="php"><span style="color: #0000ff;">$this</span>-><span style="color: #006600;">load</span>-><span style="color: #006600;">database</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;</pre>
</div>
<p>在每个控制器、模型或者其它类的构造函数里放入这条命令。之后,你就可以直接开始查询了。连接数据被保存在你的数据库config文件中,CI每次都会去那里查询它。</p>
<p>因此, 在每个CI函数中,你可以直接进行数据库查询。上面的query在CI中被转换成:</p>
<div class="code">
<div class="title">
<div style="float:right"><img class="copyCodeImage" src="images/copycode.gif" alt="" /><a href="javascript:void(0)" onclick="CopyCode(this)">复制代码到剪贴板</a></div>
<div style="clear:none">PHP 代码</div>
</div>
<pre class="php"><span style="color: #0000ff;">$query</span> = <span style="color: #0000ff;">$this</span>-><span style="color: #006600;">db</span>-><span style="color: #006600;">get</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'sites'</span><span style="color: #66cc66;">)</span>;
<span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">(</span><span style="color: #0000ff;">$query</span>-><span style="color: #006600;">result</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$row</span><span style="color: #66cc66;">)</span>
<span style="color: #66cc66;">{</span>
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #0000ff;">$row</span>-><span style="color: #006600;">url</span>
<span style="color: #66cc66;">}</span></pre>
</div>
<p>很简单, 不是吗?</p>
<p>这一个章的余下部分给出不同query的用法。</p>
<a name="C_4_3_2"></a>
<h2>4.3.2 <span class="songti">“</span>读取<span class="songti">”</span>查询</h2>
<p>常用的查询是取回来自数据库的数据。等同于select的查询是:</p>
<div class="code">
<div class="title">
<div style="float:right"><img class="copyCodeImage" src="images/copycode.gif" alt="" /><a href="javascript:void(0)" onclick="CopyCode(this)">复制代码到剪贴板</a></div>
<div style="clear:none">PHP 代码</div>
</div>
<pre class="php"><span style="color: #0000ff;">$query</span> = <span style="color: #0000ff;">$this</span>-><span style="color: #006600;">db</span>-><span style="color: #006600;">get</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'sites'</span><span style="color: #66cc66;">)</span>;</pre>
</div>
<p>这是一个“select *”查询,目标是site表。换句话说,它取回所有的行。如果你偏爱分开来写,你能这样做:</p>
<div class="code">
<div class="title">
<div style="float:right"><img class="copyCodeImage" src="images/copycode.gif" alt="" /><a href="javascript:void(0)" onclick="CopyCode(this)">复制代码到剪贴板</a></div>
<div style="clear:none">PHP 代码</div>
</div>
<pre class="php"><span style="color: #0000ff;">$this</span>-><span style="color: #006600;">db</span>-><span style="color: #006600;">from</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'sites'</span><span style="color: #66cc66;">)</span>;
<span style="color: #0000ff;">$query</span> = <span style="color: #0000ff;">$this</span>-><span style="color: #006600;">db</span>-><span style="color: #006600;">get</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;</pre>
</div>
<p>如果你想要得到特定的列,而不是全部列,这样做:</p>
<div class="code">
<div class="title">
<div style="float:right"><img class="copyCodeImage" src="images/copycode.gif" alt="" /><a href="javascript:void(0)" onclick="CopyCode(this)">复制代码到剪贴板</a></div>
<div style="clear:none">PHP 代码</div>
</div>
<pre class="php"><span style="color: #0000ff;">$this</span>-><span style="color: #006600;">db</span>-><span style="color: #006600;">select</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'url, name, clientid'</span><span style="color: #66cc66;">)</span>;
<span style="color: #0000ff;">$query</span> = <span style="color: #0000ff;">$this</span>-><span style="color: #006600;">db</span>-><span style="color: #006600;">get</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'sites'</span><span style="color: #66cc66;">)</span>;</pre>
</div>
<p>你可能要对结果排序,你可以在get语句前插入:</p>
<div class="code">
<div class="title">
<div style="float:right"><img class="copyCodeImage" src="images/copycode.gif" alt="" /><a href="javascript:void(0)" onclick="CopyCode(this)">复制代码到剪贴板</a></div>
<div style="clear:none">PHP 代码</div>
</div>
<pre class="php"><span style="color: #0000ff;">$this</span>-><span style="color: #006600;">db</span>-><span style="color: #006600;">orderby</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"name"</span>, <span style="color: #ff0000;">"desc"</span><span style="color: #66cc66;">)</span>;</pre>
</div>
<p>desc是降序的意思。你也能选择asc(升序) 或rand(random随机)。</p>
<p>你也可能想要限制返回的行数,比如你想要最初五个结果。你可以在get语句前插入:</p>
<div class="code">
<div class="title">
<div style="float:right"><img class="copyCodeImage" src="images/copycode.gif" alt="" /><a href="javascript:void(0)" onclick="CopyCode(this)">复制代码到剪贴板</a></div>
<div style="clear:none">PHP 代码</div>
</div>
<pre class="php"><span style="color: #0000ff;">$this</span>-><span style="color: #006600;">db</span>-><span style="color: #006600;">limit</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">)</span>;</pre>
</div>
<p>当然,在大多数的查询,你不可能在表中返回所有记录。数据库的具有按给定条件过滤返回结果的能力。这通常使用where子句来实现,CI这样表达:</p>
<div class="code">
<div class="title">
<div style="float:right"><img class="copyCodeImage" src="images/copycode.gif" alt="" /><a href="javascript:void(0)" onclick="CopyCode(this)">复制代码到剪贴板</a></div>
<div style="clear:none">PHP 代码</div>
</div>
<pre class="php"><span style="color: #0000ff;">$this</span>-><span style="color: #006600;">db</span>-><span style="color: #006600;">where</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'clientid'</span>, <span style="color: #ff0000;">'1'</span><span style="color: #66cc66;">)</span>;</pre>
</div>
<p>这条语句会查找客户号是 1 的客户相连的所有的网站。 但是这对我们并不是很有帮助,我们并不会记住人员表的ID号。对人来说,用姓名是比较合理的办法,因此我们需要连接到people表:</p>
<div class="code">
<div class="title">
<div style="float:right"><img class="copyCodeImage" src="images/copycode.gif" alt="" /><a href="javascript:void(0)" onclick="CopyCode(this)">复制代码到剪贴板</a></div>
<div style="clear:none">PHP 代码</div>
</div>
<pre class="php"><span style="color: #0000ff;">$this</span>-><span style="color: #006600;">db</span>-><span style="color: #006600;">from</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'sites'</span><span style="color: #66cc66;">)</span>;
<span style="color: #0000ff;">$this</span>-><span style="color: #006600;">db</span>-><span style="color: #006600;">join</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'people'</span>, <span style="color: #ff0000;">'sites.peopleid = people.id'</span><span style="color: #66cc66;">)</span>;</pre>
</div>
<p>用 sites 表中的每个 people ID 去查询这个 ID 在 people 表中的信息。</p>
<p class="important">注意SQL的约定,如果一个列名在二张表中是重复的,你需要在列名前加上表名和一个“."号。因此sites.peopleid在位置桌子中意谓peopleid所在的表是sites。在进行SQL多表查询时,最好把列名进行唯一性的标识,这样可以避免产生岐义,也可以让你自己明了。</p>
<p>你可以增加更多的where子句的操作符。举例来说,增加否定操作符:</p>
<div class="code">
<div class="title">
<div style="float:right"><img class="copyCodeImage" src="images/copycode.gif" alt="" /><a href="javascript:void(0)" onclick="CopyCode(this)">复制代码到剪贴板</a></div>
<div style="clear:none">PHP 代码</div>
</div>
<pre class="php"><span style="color: #0000ff;">$this</span>-><span style="color: #006600;">db</span>-><span style="color: #006600;">where</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'url !='</span>, <span style="color: #ff0000;">'www.mysite.com'</span><span style="color: #66cc66;">)</span>;</pre>
</div>
<p>或比较操作符:</p>
<div class="code">
<div class="title">
<div style="float:right"><img class="copyCodeImage" src="images/copycode.gif" alt="" /><a href="javascript:void(0)" onclick="CopyCode(this)">复制代码到剪贴板</a></div>
<div style="clear:none">PHP 代码</div>
</div>
<pre class="php"><span style="color: #0000ff;">$this</span>-><span style="color: #006600;">db</span>-><span style="color: #006600;">where</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'id >'</span>, <span style="color: #ff0000;">'3'</span><span style="color: #66cc66;">)</span>;</pre>
</div>
<p>或组合语句(“WHERE…AND…”):</p>
<div class="code">
<div class="title">
<div style="float:right"><img class="copyCodeImage" src="images/copycode.gif" alt="" /><a href="javascript:void(0)" onclick="CopyCode(this)">复制代码到剪贴板</a></div>
<div style="clear:none">PHP 代码</div>
</div>
<pre class="php"><span style="color: #0000ff;">$this</span>-><span style="color: #006600;">db</span>-><span style="color: #006600;">where</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'url !='</span>,<span style="color: #ff0000;">'www.mysite.com'</span><span style="color: #66cc66;">)</span>;
<span style="color: #0000ff;">$this</span>-><span style="color: #006600;">db</span>-><span style="color: #006600;">where</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'id >'</span>, <span style="color: #ff0000;">'3'</span><span style="color: #66cc66;">)</span>;</pre>
</div>
<p>或使用 $this->db->orwhere(); 来表示(“WHERE…OR”):</p>
<div class="code">
<div class="title">
<div style="float:right"><img class="copyCodeImage" src="images/copycode.gif" alt="" /><a href="javascript:void(0)" onclick="CopyCode(this)">复制代码到剪贴板</a></div>
<div style="clear:none">PHP 代码</div>
</div>
<pre class="php"><span style="color: #0000ff;">$this</span>-><span style="color: #006600;">db</span>-><span style="color: #006600;">where</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'url !='</span>,<span style="color: #ff0000;">'www.mysite.com'</span> <span style="color: #66cc66;">)</span>;
<span style="color: #0000ff;">$this</span>-><span style="color: #006600;">db</span>-><span style="color: #006600;">orwhere</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'url !='</span>,<span style="color: #ff0000;">'www.anothersite.com'</span> <span style="color: #66cc66;">)</span>;</pre>
</div>
<p>现在让我们建立一个完整的查询:</p>
<div class="code">
<div class="title">
<div style="float:right"><img class="copyCodeImage" src="images/copycode.gif" alt="" /><a href="javascript:void(0)" onclick="CopyCode(this)">复制代码到剪贴板</a></div>
<div style="clear:none">PHP 代码</div>
</div>
<pre class="php"><span style="color: #0000ff;">$this</span>-><span style="color: #006600;">db</span>-><span style="color: #006600;">select</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'url'</span>,<span style="color: #ff0000;">'name'</span>,<span style="color: #ff0000;">'clientid'</span>,<span style="color: #ff0000;">'people.surname AS client'</span><span style="color: #66cc66;">)</span>;
<span style="color: #0000ff;">$this</span>-><span style="color: #006600;">db</span>-><span style="color: #006600;">where</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'clientid'</span>, <span style="color: #ff0000;">'3'</span><span style="color: #66cc66;">)</span>;
<span style="color: #0000ff;">$this</span>-><span style="color: #006600;">db</span>-><span style="color: #006600;">limit</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">)</span>;
<span style="color: #0000ff;">$this</span>-><span style="color: #006600;">db</span>-><span style="color: #006600;">from</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'sites'</span><span style="color: #66cc66;">)</span>;
<span style="color: #0000ff;">$this</span>-><span style="color: #006600;">db</span>-><span style="color: #006600;">join</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'people'</span>, <span style="color: #ff0000;">'sites.clientid = people.id'</span><span style="color: #66cc66;">)</span>;
<span style="color: #0000ff;">$this</span>-><span style="color: #006600;">db</span>-><span style="color: #006600;">orderby</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"name"</span>, <span style="color: #ff0000;">"desc"</span><span style="color: #66cc66;">)</span>;
<span style="color: #0000ff;">$query</span> = <span style="color: #0000ff;">$this</span>-><span style="color: #006600;">db</span>-><span style="color: #006600;">get</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;</pre>
</div>
<p>这应该给我们前五个(用姓名排序)网站,这些网站属于3号客户,而且还显示客户的姓和他或她的身份证数字!</p>
<p>使用AR的潜在好处是已经进行了自动的转义,因此,你不必关心转义的问题。这适用于这样的函数像$this->db->where(),以及在下一个段中被描述的数据插入和修改语句。(安全警告:这不同于阻止交叉脚本攻击-对付这个你需要CI的xss_clean()函数。它也不相同于验证你的数据-对付这个你需要 CI 的验证类,见第五章。)</p>
<a name="C_4_3_3"></a>
<h2>4.3.3 显示查询结果</h2>
<p>在CI显示查询结果相当简单。假定我们定义了上述的查询语句, 最后一句是:</p>
<div class="code">
<div class="title">
<div style="float:right"><img class="copyCodeImage" src="images/copycode.gif" alt="" /><a href="javascript:void(0)" onclick="CopyCode(this)">复制代码到剪贴板</a></div>
<div style="clear:none">PHP 代码</div>
</div>
<pre class="php"><span style="color: #0000ff;">$query</span> = <span style="color: #0000ff;">$this</span>-><span style="color: #006600;">db</span>-><span style="color: #006600;">get</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;</pre>
</div>
<p>然后,如果有多个结果,他们被保存在$row对象中,你可以用一个 foreach 循环:</p>
<div class="code">
<div class="title">
<div style="float:right"><img class="copyCodeImage" src="images/copycode.gif" alt="" /><a href="javascript:void(0)" onclick="CopyCode(this)">复制代码到剪贴板</a></div>
<div style="clear:none">PHP 代码</div>
</div>
<pre class="php"><span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">(</span><span style="color: #0000ff;">$query</span>-><span style="color: #006600;">result</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$row</span><span style="color: #66cc66;">)</span>
<span style="color: #66cc66;">{</span>
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #0000ff;">$row</span>-><span style="color: #006600;">url</span>;
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #0000ff;">$row</span>-><span style="color: #006600;">name</span>;
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #0000ff;">$row</span>-><span style="color: #006600;">client</span>;
<span style="color: #66cc66;">}</span></pre>
</div>
<p>或如果我们只想要一个结果,它可以作为一个对象被返回, 或在这里当做一个$row数组:</p>
<div class="code">
<div class="title">
<div style="float:right"><img class="copyCodeImage" src="images/copycode.gif" alt="" /><a href="javascript:void(0)" onclick="CopyCode(this)">复制代码到剪贴板</a></div>
<div style="clear:none">PHP 代码</div>
</div>
<pre class="php"><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span><span style="color: #0000ff;">$query</span>-><span style="color: #006600;">num_rows</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span> > <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">)</span>
<span style="color: #66cc66;">{</span>
<span style="color: #0000ff;">$row</span> = <span style="color: #0000ff;">$query</span>-><span style="color: #006600;">row_array</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #0000ff;">$row</span><span style="color: #66cc66;">[</span><span style="color: #ff0000;">'url'</span><span style="color: #66cc66;">]</span>;
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #0000ff;">$row</span><span style="color: #66cc66;">[</span><span style="color: #ff0000;">'name'</span><span style="color: #66cc66;">]</span>;
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #0000ff;">$row</span><span style="color: #66cc66;">[</span><span style="color: #ff0000;">'client'</span><span style="color: #66cc66;">]</span>;
<span style="color: #66cc66;">}</span></pre>
</div>
<p>我比较喜欢对象语法胜过数组-更简洁!</p>
<p>如果你遵守 MVC 模式,你将会在模型中保存你的查询和数据库交互, 然后通过视图显示数据。</p>
<a name="C_4_3_4"></a>
<h2>4.3.4 <span class="songti">“</span>创建<span class="songti">”</span>和<span class="songti">“</span>更新<span class="songti">”</span>查询</h2>
<p>AR 有三个函数帮助你在数据库内生成新的实体,它们是$this->db->insert(),$this->db->update,$this->db->set().</p>
<p>“create”和“update”的不同之处是“create”是向表中插入一条全新的记录,而“update”是修改表中已经存在的记录。因此对“update”,你必须首先定位需要修改的记录。</p>
<p>CI 用数组来保存数据,或是使用$this->db->set();你可以任选一种。</p>
<p>因此, 如果要在websites数据库中加入一行。首先,确保在我们的控制器中的构造函数加入:</p>
<div class="code">
<div class="title">
<div style="float:right"><img class="copyCodeImage" src="images/copycode.gif" alt="" /><a href="javascript:void(0)" onclick="CopyCode(this)">复制代码到剪贴板</a></div>
<div style="clear:none">PHP 代码</div>
</div>
<pre class="php"><span style="color: #0000ff;">$this</span>-><span style="color: #006600;">load</span>-><span style="color: #006600;">database</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;</pre>
</div>
<p>我们想要增加一个新的网站,包含有一个网址,一个名字,一个类型和一个客户ID。如果用数组的方式,这可能是:</p>
<div class="code">
<div class="title">
<div style="float:right"><img class="copyCodeImage" src="images/copycode.gif" alt="" /><a href="javascript:void(0)" onclick="CopyCode(this)">复制代码到剪贴板</a></div>
<div style="clear:none">PHP 代码</div>
</div>
<pre class="php"><span style="color: #0000ff;">$data</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span>
<span style="color: #ff0000;">'url'</span> => <span style="color: #ff0000;">'www.mynewclient.com'</span>,
<span style="color: #ff0000;">'name'</span> => <span style="color: #ff0000;">'BigCo Inc'</span>,
<span style="color: #ff0000;">'clientid'</span> => <span style="color: #ff0000;">'33'</span>,
<span style="color: #ff0000;">'type'</span> => <span style="color: #ff0000;">'dynamic'</span>
<span style="color: #66cc66;">)</span>;</pre>
</div>
<p>把这些信息增加到sites表,我们使用:</p>
<div class="code">
<div class="title">
<div style="float:right"><img class="copyCodeImage" src="images/copycode.gif" alt="" /><a href="javascript:void(0)" onclick="CopyCode(this)">复制代码到剪贴板</a></div>
<div style="clear:none">PHP 代码</div>
</div>
<pre class="php"><span style="color: #0000ff;">$this</span>-><span style="color: #006600;">db</span>-><span style="color: #006600;">insert</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'sites'</span>, <span style="color: #0000ff;">$data</span><span style="color: #66cc66;">)</span>;</pre>
</div>
<p>或者,我们也可以使用 $this->db->set() 设置每一个值:</p>
<div class="code">
<div class="title">
<div style="float:right"><img class="copyCodeImage" src="images/copycode.gif" alt="" /><a href="javascript:void(0)" onclick="CopyCode(this)">复制代码到剪贴板</a></div>
<div style="clear:none">PHP 代码</div>
</div>
<pre class="php"><span style="color: #0000ff;">$this</span>-><span style="color: #006600;">db</span>-><span style="color: #006600;">set</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'url'</span>, <span style="color: #ff0000;">'www.mynewclinet.com'</span><span style="color: #66cc66;">)</span>;
<span style="color: #0000ff;">$this</span>-><span style="color: #006600;">db</span>-><span style="color: #006600;">set</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'name'</span>, <span style="color: #ff0000;">'BigCo Inc'</span><span style="color: #66cc66;">)</span>;
<span style="color: #0000ff;">$this</span>-><span style="color: #006600;">db</span>-><span style="color: #006600;">set</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'clientid'</span>, <span style="color: #ff0000;">'33'</span><span style="color: #66cc66;">)</span>;
<span style="color: #0000ff;">$this</span>-><span style="color: #006600;">db</span>-><span style="color: #006600;">set</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'type'</span>, <span style="color: #ff0000;">'dynamic'</span><span style="color: #66cc66;">)</span>;
<span style="color: #0000ff;">$this</span>-><span style="color: #006600;">db</span>-><span style="color: #006600;">insert</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'sites'</span><span style="color: #66cc66;">)</span>;</pre>
</div>
<p>如果我们正在更新一笔现有的记录,我们也可以创建一个数组或使用$this->db->set(),但是现在有两个不同。</p>
<p>第一,我们必须定位我们想要更新的记录;其次,我们需要使用$this->db->set(),如果我想要在sites中更新一笔记录(针对'id'列的值1的那行记录),使用数组的方式是这样处理的:</p>
<div class="code">
<div class="title">
<div style="float:right"><img class="copyCodeImage" src="images/copycode.gif" alt="" /><a href="javascript:void(0)" onclick="CopyCode(this)">复制代码到剪贴板</a></div>
<div style="clear:none">PHP 代码</div>
</div>
<pre class="php"><span style="color: #0000ff;">$this</span>-><span style="color: #006600;">db</span>-><span style="color: #006600;">where</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'id'</span>, <span style="color: #ff0000;">'1'</span><span style="color: #66cc66;">)</span>;
<span style="color: #0000ff;">$this</span>-><span style="color: #006600;">db</span>-><span style="color: #006600;">update</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'sites'</span>, <span style="color: #0000ff;">$data</span><span style="color: #66cc66;">)</span>;</pre>
</div>
<p>你也可以使用$this->db->set()方式,就像前面做过的那样。</p>
<p>CI 提供几个函数检查数据库是否成功执行了相关操作。 最有用的:</p>
<div class="code">
<div class="title">
<div style="float:right"><img class="copyCodeImage" src="images/copycode.gif" alt="" /><a href="javascript:void(0)" onclick="CopyCode(this)">复制代码到剪贴板</a></div>
<div style="clear:none">PHP 代码</div>
</div>
<pre class="php"><span style="color: #0000ff;">$this</span>-><span style="color: #006600;">db</span>-><span style="color: #006600;">affected_rows</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;</pre>
</div>
<p>在执行insert或update后应该返回 '1'-但是如果我正在update一批记录的话,可能返回更大的一个整数。</p>
<p>你已经注意到当我insert一笔新的记录时,我没有设定ID这一列。这是因为ID这列被设定为自动插入类型。但是当我update一笔现有的记录时候,我必须引用ID属性,否则数据库不知道该改变哪一笔记录。</p>
<p>如果我正在insert一笔新的记录, 在实际产生它之前,我们并不知道ID具体的值。如果我需要引用新的记录的ID, 使用下列语句:</p>
<div class="code">
<div class="title">
<div style="float:right"><img class="copyCodeImage" src="images/copycode.gif" alt="" /><a href="javascript:void(0)" onclick="CopyCode(this)">复制代码到剪贴板</a></div>
<div style="clear:none">PHP 代码</div>
</div>
<pre class="php"><span style="color: #0000ff;">$new_id_number</span> = <span style="color: #0000ff;">$this</span>-><span style="color: #006600;">db</span>-><span style="color: #006600;">insert_id</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;</pre>
</div>
<p>(这一行代码必须跟在insert语句之后,否则可能得到错误的结果。)</p>
<p>还有一点需要知道,CI的AR操作,包括$this->db->insert_id()和$this->db->update_id()会自己转义。</p>
<p>从 1.5 版,CI也包含了对事务的支持,即指定的一批SQL操作要么全成功,要么全失败,换句话说,要么提交,要么回滚。这在复式记帐应用和许多商业应用中是很重要的。举例来说,说你正在卖电影票。你需要接受付款,同时分配座位。如果你的系统收费成功但分配座位失败,这个客户肯定是要光火的。</p>
<p>CI 现在也让事务处理变得很简单,即要么“提交”,要么回滚。你可以参考用户手册以得到更多关于事务的信息。</p>
<a name="C_4_3_5"></a>
<h2>4.3.5 <span class="songti">“</span>删除<span class="songti">”</span>查询</h2>
<p>删除操作也许是最简单的。你只要定位好需要删除记录,比如我们要删除ID为2的记录:</p>
<div class="code">
<div class="title">
<div style="float:right"><img class="copyCodeImage" src="images/copycode.gif" alt="" /><a href="javascript:void(0)" onclick="CopyCode(this)">复制代码到剪贴板</a></div>
<div style="clear:none">PHP 代码</div>
</div>
<pre class="php"><span style="color: #0000ff;">$this</span>-><span style="color: #006600;">db</span>-><span style="color: #006600;">where</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'id'</span>, <span style="color: #ff0000;">'2'</span><span style="color: #66cc66;">)</span>;
<span style="color: #0000ff;">$this</span>-><span style="color: #006600;">db</span>-><span style="color: #006600;">delete</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'sites'</span><span style="color: #66cc66;">)</span>;</pre>
</div>
<p>要小心使用删除操作,因为如果你不注意where中的条件,可能会误操作,甚至最坏的结果是删除整张表。</p>
<a name="C_4_3_6"></a>
<h2>4.3.6 Active Record 和传统 SQL 编程的结合</h2>
<p>CI不要求你只能使用AR,你也能用CI直接发送SQL查询。比如:假定你已在构造函数里已连接了数据库,你可以在需要的地方直接使用类似的SQL查询:</p>
<div class="code">
<div class="title">
<div style="float:right"><img class="copyCodeImage" src="images/copycode.gif" alt="" /><a href="javascript:void(0)" onclick="CopyCode(this)">复制代码到剪贴板</a></div>
<div style="clear:none">PHP 代码</div>
</div>
<pre class="php"><span style="color: #0000ff;">$this</span>-><span style="color: #006600;">db</span>-><span style="color: #006600;">query</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"SELECT id, name, url FROM sites WHERE 'type' = 'dynamic'"</span><span style="color: #66cc66;">)</span>;</pre>
</div>
<p>我个人觉得AR更容易使用。借助数组为AR准备数据更直观,更容易理解,虽然可能需要更多的代码。AR还能自动转义,对不熟悉SQL语法的程序员会更有吸引力。</p>
<p>然而,有那么少数几种情况,可能你需要使用原始的 SQL 语句。比如你想使用复杂的 join,或者又比如你需要使用多个“where”条件。如果你想找出 client 为 3 的网站,并且只是这两个指定的 type,那么你需要给 SQL 语句加上括号以便查询能被正确执行。</p>
<p>在这些情况下,你可以随时把 SQL 作为一个字符串,并放入一个变量中,然后在 CI 的 $this->db->where() 函数中使用这个变量:</p>
<div class="code">
<div class="title">
<div style="float:right"><img class="copyCodeImage" src="images/copycode.gif" alt="" /><a href="javascript:void(0)" onclick="CopyCode(this)">复制代码到剪贴板</a></div>
<div style="clear:none">PHP 代码</div>
</div>
<pre class="php"><span style="color: #0000ff;">$condition</span> = <span style="color: #ff0000;">"client ='3' AND (type ='dynamic' OR type='static')"</span>;
<span style="color: #0000ff;">$this</span>-><span style="color: #006600;">db</span>-><span style="color: #006600;">where</span><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$condition</span><span style="color: #66cc66;">)</span>;</pre>
</div>
<p>如果没有括号就是含糊不清的。你的意思是:</p>
<div class="code">
<div class="title">
<div style="float:right"><img class="copyCodeImage" src="images/copycode.gif" alt="" /><a href="javascript:void(0)" onclick="CopyCode(this)">复制代码到剪贴板</a></div>
<div style="clear:none">SQL 代码</div>
</div>
<pre class="sql"><span style="color: #66cc66;">(</span>client=<span style="color: #ff0000;">'3'</span> <span style="color: #993333; font-weight: bold;">AND</span> type = <span style="color: #ff0000;">'dynamic'</span><span style="color: #66cc66;">)</span> <span style="color: #993333; font-weight: bold;">OR</span> type = <span style="color: #ff0000;">'static'</span></pre>
</div>
<p>或</p>
<div class="code">
<div class="title">
<div style="float:right"><img class="copyCodeImage" src="images/copycode.gif" alt="" /><a href="javascript:void(0)" onclick="CopyCode(this)">复制代码到剪贴板</a></div>
<div style="clear:none">SQL 代码</div>
</div>
<pre class="sql">client=<span style="color: #ff0000;">'3'</span> <span style="color: #993333; font-weight: bold;">AND</span> <span style="color: #66cc66;">(</span>type = <span style="color: #ff0000;">'dynamic'</span> <span style="color: #993333; font-weight: bold;">OR</span> type = <span style="color: #ff0000;">'static'</span><span style="color: #66cc66;">)</span></pre>
</div>
<p>嗯,是的,当然,这是显而易见的,但机器通常的猜测是错误的。顺便说一句,小心 $condition 的语法。实际的 SQL 查询是:</p>
<div class="code">
<div class="title">
<div style="float:right"><img class="copyCodeImage" src="images/copycode.gif" alt="" /><a href="javascript:void(0)" onclick="CopyCode(this)">复制代码到剪贴板</a></div>
<div style="clear:none">SQL 代码</div>
</div>
<pre class="sql">client=<span style="color: #ff0000;">'3'</span> <span style="color: #993333; font-weight: bold;">AND</span> <span style="color: #66cc66;">(</span>type = <span style="color: #ff0000;">'dynamic'</span> <span style="color: #993333; font-weight: bold;">OR</span> type = <span style="color: #ff0000;">'static'</span><span style="color: #66cc66;">)</span></pre>
</div>
<p>双引号是用来定义变量的:</p>
<div class="code">
<div class="title">
<div style="float:right"><img class="copyCodeImage" src="images/copycode.gif" alt="" /><a href="javascript:void(0)" onclick="CopyCode(this)">复制代码到剪贴板</a></div>
<div style="clear:none">PHP 代码</div>
</div>
<pre class="php"><span style="color: #0000ff;">$condition</span> = <span style="color: #ff0000;">" "</span>;</pre>
</div>
<p>这很容易让你的单引号和双引号产生混淆。</p>
<p>我上面描述的一些 CI 表达式,比如 $this->db->affected_rows(),并不是 Active Record 模型的一部分。但他们可以很容易的混合使用。</p>
<p>需要指出的是,如果你试着混合使用 AR 和直接的 SQL 查询,你可能会有麻烦。(我还没有尝试过。如果你手中有大把的时间,你可以对其进行测试,但坦率地说,我认为这表明一个不太好的生活方式。如果你有这闲工夫,不如尝试去数火车厢,至少还能让你呼吸点新鲜空气。而我本人,则是太忙了以至于不能不用 CI!)</p>
<a name="C_4_4"></a>
<h2>4.4 总结</h2>
<p>我们已经介绍了 CI 的 Active Record 类,而且见到,它是多么容易使用:</p>
<ul>
<li>连接到一个或更多的数据库</li>
<li>编写标准 SQL 的“读取”、“更新”、“创建”和“删除”查询</li>
<li>正确使用数据库所需要执行的其他功能</li>
</ul>
<p>CI 的 AR 功能概念清晰又容易使用,而且使代码非常易读。它自动化数据库连接,并把连接数据保存至一个 config 文件。</p>
<p>它几乎可以做“传统”SQL 所能做的任何事情—比我在这里讲的要多的多。更多内容请参考在线《用户指南》。</p>
<a name="C_4_5"></a>
<h2>4.5 附录:使用 MYSQL 查询语句创建<span class="songti">“</span>websites<span class="songti">”</span>数据库</h2>
<div class="code">
<div class="title">
<div style="float:right"><img class="copyCodeImage" src="images/copycode.gif" alt="" /><a href="javascript:void(0)" onclick="CopyCode(this)">复制代码到剪贴板</a></div>
<div style="clear:none">MYSQL 代码</div>
</div>
<pre class="mysql"><span style="color: #993333; font-weight: bold;">DROP TABLE</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> `ci_sessions`;
<span style="color: #993333; font-weight: bold;">CREATE TABLE</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #aa3399; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> `ci_sessions` <span style="color: #66cc66;">(</span>
`session_id` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">40</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span> <span style="color: #aa3399; font-weight: bold;">DEFAULT</span> <span style="color: #ff0000;">'0'</span>,
`peopleid` <span style="color: #aa9933; font-weight: bold;">INT</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`ip_address` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">16</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span> <span style="color: #aa3399; font-weight: bold;">DEFAULT</span> <span style="color: #ff0000;">'0'</span>,
`user_agent` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`last_activity` <span style="color: #aa9933; font-weight: bold;">INT</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">UNSIGNED</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span> <span style="color: #aa3399; font-weight: bold;">DEFAULT</span> <span style="color: #ff0000;">'0'</span>,
`<span style="color: #993333; font-weight: bold;">LEFT</span>` <span style="color: #aa9933; font-weight: bold;">INT</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`name` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">25</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`status` <span style="color: #aa9933; font-weight: bold;">TINYINT</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">4</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span> <span style="color: #aa3399; font-weight: bold;">DEFAULT</span> <span style="color: #ff0000;">'0'</span>
<span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">ENGINE</span>=MyISAM <span style="color: #aa3399; font-weight: bold;">DEFAULT</span> <span style="color: #aa3399; font-weight: bold;">CHARSET</span>=utf8;
<span style="color: #993333; font-weight: bold;">DROP TABLE</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> `domains`;
<span style="color: #993333; font-weight: bold;">CREATE TABLE</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #aa3399; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> `domains` <span style="color: #66cc66;">(</span>
`id` <span style="color: #aa9933; font-weight: bold;">INT</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span> <span style="color: #aa3399; font-weight: bold;">AUTO_INCREMENT</span>,
`url` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">100</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`name` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">100</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`registrar` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">100</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`dateregd` <span style="color: #aa9933; font-weight: bold;">INT</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span> <span style="color: #aa3399; font-weight: bold;">DEFAULT</span> <span style="color: #ff0000;">'0'</span>,
`cost` <span style="color: #aa9933; font-weight: bold;">FLOAT</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span> <span style="color: #aa3399; font-weight: bold;">DEFAULT</span> <span style="color: #ff0000;">'0'</span>,
`regdfor` <span style="color: #aa9933; font-weight: bold;">INT</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span> <span style="color: #aa3399; font-weight: bold;">DEFAULT</span> <span style="color: #ff0000;">'0'</span>,
`notes` <span style="color: #aa9933; font-weight: bold;">BLOB</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`pw` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">25</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`un` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">25</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`lastupdate` <span style="color: #aa9933; font-weight: bold;">INT</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span> <span style="color: #aa3399; font-weight: bold;">DEFAULT</span> <span style="color: #ff0000;">'0'</span>,
`submit` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">25</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
<span style="color: #993333; font-weight: bold;">PRIMARY KEY</span> <span style="color: #66cc66;">(</span>`id`<span style="color: #66cc66;">)</span>
<span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">ENGINE</span>=MyISAM <span style="color: #aa3399; font-weight: bold;">DEFAULT</span> <span style="color: #aa3399; font-weight: bold;">CHARSET</span>=utf8 <span style="color: #aa3399; font-weight: bold;">AUTO_INCREMENT</span>=<span style="color: #cc66cc;">10</span>;
<span style="color: #993333; font-weight: bold;">DROP TABLE</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> `events`;
<span style="color: #993333; font-weight: bold;">CREATE TABLE</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #aa3399; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> `events` <span style="color: #66cc66;">(</span>
`id` <span style="color: #aa9933; font-weight: bold;">INT</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span> <span style="color: #aa3399; font-weight: bold;">AUTO_INCREMENT</span>,
`name` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span> <span style="color: #aa3399; font-weight: bold;">DEFAULT</span> <span style="color: #ff0000;">'not set'</span>,
`type` <span style="color: #aa9933; font-weight: bold;">ENUM</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'test'</span>,<span style="color: #ff0000;">'alert'</span>,<span style="color: #ff0000;">'report'</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`testid` <span style="color: #aa9933; font-weight: bold;">INT</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`siteid` <span style="color: #aa9933; font-weight: bold;">INT</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`userid` <span style="color: #aa9933; font-weight: bold;">INT</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`reported` <span style="color: #aa9933; font-weight: bold;">INT</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`result` <span style="color: #aa9933; font-weight: bold;">BLOB</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`<span style="color: #993333; font-weight: bold;">TIME</span>` <span style="color: #aa9933; font-weight: bold;">INT</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`timetaken` <span style="color: #aa9933; font-weight: bold;">FLOAT</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`isalert` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`emailid` <span style="color: #aa9933; font-weight: bold;">INT</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`submit` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">25</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
<span style="color: #993333; font-weight: bold;">PRIMARY KEY</span> <span style="color: #66cc66;">(</span>`id`<span style="color: #66cc66;">)</span>
<span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">ENGINE</span>=MyISAM <span style="color: #aa3399; font-weight: bold;">DEFAULT</span> <span style="color: #aa3399; font-weight: bold;">CHARSET</span>=utf8 <span style="color: #aa3399; font-weight: bold;">AUTO_INCREMENT</span>=<span style="color: #cc66cc;">69</span>;
<span style="color: #993333; font-weight: bold;">DROP TABLE</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> `frequencies`;
<span style="color: #993333; font-weight: bold;">CREATE TABLE</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #aa3399; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> `frequencies` <span style="color: #66cc66;">(</span>
`id` <span style="color: #aa9933; font-weight: bold;">INT</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`name` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">16</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`submit` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">25</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
<span style="color: #993333; font-weight: bold;">PRIMARY KEY</span> <span style="color: #66cc66;">(</span>`id`<span style="color: #66cc66;">)</span>
<span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">ENGINE</span>=MyISAM <span style="color: #aa3399; font-weight: bold;">DEFAULT</span> <span style="color: #aa3399; font-weight: bold;">CHARSET</span>=utf8;
<span style="color: #993333; font-weight: bold;">DROP TABLE</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> `hosts`;
<span style="color: #993333; font-weight: bold;">CREATE TABLE</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #aa3399; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> `hosts` <span style="color: #66cc66;">(</span>
`id` <span style="color: #aa9933; font-weight: bold;">INT</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span> <span style="color: #aa3399; font-weight: bold;">AUTO_INCREMENT</span>,
`cost` <span style="color: #aa9933; font-weight: bold;">FLOAT</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`name` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">100</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`hosturl` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">100</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`un` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`pw` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`ns1url` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">36</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`ns1ip` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">36</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`ns2url` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">36</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`ns2ip` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">36</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`ftpurl` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">100</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`ftpserverip` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">36</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`ftpun` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`ftppw` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`cpurl` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">36</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`cpun` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">36</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`cppw` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">36</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`pop3server` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">36</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`servicetel` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`servicetel2` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`serviceemail` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">100</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`webroot` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">48</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`absoluteroot` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">48</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`cgiroot` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">48</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`booked` <span style="color: #aa9933; font-weight: bold;">INT</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`duration` <span style="color: #aa9933; font-weight: bold;">INT</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`lastupdate` <span style="color: #aa9933; font-weight: bold;">INT</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span> <span style="color: #aa3399; font-weight: bold;">DEFAULT</span> <span style="color: #ff0000;">'0'</span>,
`submit` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">25</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
<span style="color: #993333; font-weight: bold;">PRIMARY KEY</span> <span style="color: #66cc66;">(</span>`id`<span style="color: #66cc66;">)</span>
<span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">ENGINE</span>=MyISAM <span style="color: #aa3399; font-weight: bold;">DEFAULT</span> <span style="color: #aa3399; font-weight: bold;">CHARSET</span>=utf8 <span style="color: #aa3399; font-weight: bold;">AUTO_INCREMENT</span>=<span style="color: #cc66cc;">6</span>;
<span style="color: #993333; font-weight: bold;">DROP TABLE</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> `people`;
<span style="color: #993333; font-weight: bold;">CREATE TABLE</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #aa3399; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> `people` <span style="color: #66cc66;">(</span>
`id` <span style="color: #aa9933; font-weight: bold;">INT</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span> <span style="color: #aa3399; font-weight: bold;">AUTO_INCREMENT</span>,
`uname` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">25</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`pw` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">25</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`status` <span style="color: #aa9933; font-weight: bold;">SMALLINT</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span> <span style="color: #aa3399; font-weight: bold;">DEFAULT</span> <span style="color: #ff0000;">'1'</span>,
`name` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`firstname` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`surname` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`email` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">120</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`lastupdate` <span style="color: #aa9933; font-weight: bold;">INT</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span> <span style="color: #aa3399; font-weight: bold;">DEFAULT</span> <span style="color: #ff0000;">'0'</span>,
`submit` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">25</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
<span style="color: #993333; font-weight: bold;">PRIMARY KEY</span> <span style="color: #66cc66;">(</span>`id`<span style="color: #66cc66;">)</span>
<span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">ENGINE</span>=MyISAM <span style="color: #aa3399; font-weight: bold;">DEFAULT</span> <span style="color: #aa3399; font-weight: bold;">CHARSET</span>=utf8 <span style="color: #aa3399; font-weight: bold;">AUTO_INCREMENT</span>=<span style="color: #cc66cc;">5</span>;
<span style="color: #993333; font-weight: bold;">DROP TABLE</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> `sites`;
<span style="color: #993333; font-weight: bold;">CREATE TABLE</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #aa3399; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> `sites` <span style="color: #66cc66;">(</span>
`id` <span style="color: #aa9933; font-weight: bold;">INT</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span> <span style="color: #aa3399; font-weight: bold;">AUTO_INCREMENT</span>,
`name` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">100</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`url` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">100</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`un` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`pw` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`client1` <span style="color: #aa9933; font-weight: bold;">INT</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span> <span style="color: #aa3399; font-weight: bold;">DEFAULT</span> <span style="color: #ff0000;">'0'</span>,
`client2` <span style="color: #aa9933; font-weight: bold;">INT</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span> <span style="color: #aa3399; font-weight: bold;">DEFAULT</span> <span style="color: #ff0000;">'0'</span>,
`admin1` <span style="color: #aa9933; font-weight: bold;">INT</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span> <span style="color: #aa3399; font-weight: bold;">DEFAULT</span> <span style="color: #ff0000;">'0'</span>,
`admin2` <span style="color: #aa9933; font-weight: bold;">INT</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span> <span style="color: #aa3399; font-weight: bold;">DEFAULT</span> <span style="color: #ff0000;">'0'</span>,
`domainid` <span style="color: #aa9933; font-weight: bold;">INT</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span> <span style="color: #aa3399; font-weight: bold;">DEFAULT</span> <span style="color: #ff0000;">'0'</span>,
`hostid` <span style="color: #aa9933; font-weight: bold;">INT</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span> <span style="color: #aa3399; font-weight: bold;">DEFAULT</span> <span style="color: #ff0000;">'0'</span>,
`webroot` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`files` <span style="color: #aa9933; font-weight: bold;">TEXT</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`filesdate` <span style="color: #aa9933; font-weight: bold;">INT</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span> <span style="color: #aa3399; font-weight: bold;">DEFAULT</span> <span style="color: #ff0000;">'0'</span>,
`lastupdate` <span style="color: #aa9933; font-weight: bold;">INT</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span> <span style="color: #aa3399; font-weight: bold;">DEFAULT</span> <span style="color: #ff0000;">'0'</span>,
`submit` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">25</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
<span style="color: #993333; font-weight: bold;">PRIMARY KEY</span> <span style="color: #66cc66;">(</span>`id`<span style="color: #66cc66;">)</span>
<span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">ENGINE</span>=MyISAM <span style="color: #aa3399; font-weight: bold;">DEFAULT</span> <span style="color: #aa3399; font-weight: bold;">CHARSET</span>=utf8 <span style="color: #aa3399; font-weight: bold;">AUTO_INCREMENT</span>=<span style="color: #cc66cc;">15</span>;
<span style="color: #993333; font-weight: bold;">DROP TABLE</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> `tests`;
<span style="color: #993333; font-weight: bold;">CREATE TABLE</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #aa3399; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> `tests` <span style="color: #66cc66;">(</span>
`id` <span style="color: #aa9933; font-weight: bold;">INT</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span> <span style="color: #aa3399; font-weight: bold;">AUTO_INCREMENT</span>,
`siteid` <span style="color: #aa9933; font-weight: bold;">INT</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span> <span style="color: #aa3399; font-weight: bold;">DEFAULT</span> <span style="color: #ff0000;">'0'</span>,
`name` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">250</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`type` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">25</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`url` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">120</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`regex` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">250</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`p1` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">250</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`p2` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">250</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`p3` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">250</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`p4` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">250</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`p5` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">250</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`p6` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">250</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`frequency` <span style="color: #aa9933; font-weight: bold;">INT</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span> <span style="color: #aa3399; font-weight: bold;">DEFAULT</span> <span style="color: #ff0000;">'0'</span>,
`lastdone` <span style="color: #aa9933; font-weight: bold;">INT</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span> <span style="color: #aa3399; font-weight: bold;">DEFAULT</span> <span style="color: #ff0000;">'0'</span>,
`isalert` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`setup` <span style="color: #aa9933; font-weight: bold;">INT</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span> <span style="color: #aa3399; font-weight: bold;">DEFAULT</span> <span style="color: #ff0000;">'0'</span>,
`lastupdate` <span style="color: #aa9933; font-weight: bold;">INT</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span> <span style="color: #aa3399; font-weight: bold;">DEFAULT</span> <span style="color: #ff0000;">'0'</span>,
`notes` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">250</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`submit` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">25</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
<span style="color: #993333; font-weight: bold;">PRIMARY KEY</span> <span style="color: #66cc66;">(</span>`id`<span style="color: #66cc66;">)</span>
<span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">ENGINE</span>=MyISAM <span style="color: #aa3399; font-weight: bold;">DEFAULT</span> <span style="color: #aa3399; font-weight: bold;">CHARSET</span>=utf8 <span style="color: #aa3399; font-weight: bold;">AUTO_INCREMENT</span>=<span style="color: #cc66cc;">11</span>;
<span style="color: #993333; font-weight: bold;">DROP TABLE</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> `types`;
<span style="color: #993333; font-weight: bold;">CREATE TABLE</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #aa3399; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> `types` <span style="color: #66cc66;">(</span>
`id` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">7</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
`name` <span style="color: #aa9933; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">(</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">NOT NULL</span>,
<span style="color: #993333; font-weight: bold;">PRIMARY KEY</span> <span style="color: #66cc66;">(</span>`id`<span style="color: #66cc66;">)</span>
<span style="color: #66cc66;">)</span> <span style="color: #aa3399; font-weight: bold;">ENGINE</span>=MyISAM <span style="color: #aa3399; font-weight: bold;">DEFAULT</span> <span style="color: #aa3399; font-weight: bold;">CHARSET</span>=utf8;</pre>
</div>
</div>
</div>
<a name="bottom"></a>
<div id="footer">
<div class="title"><a href="3.html">上一页</a> | <a href="5.html">下一页</a> | <a href="table_of_contents.html">目录</a> | <a href="#top">转到页首</a></div>
<div style="clear:none">第四章 使用 CI 简化数据库开发</div>
</div>
</div>
</body>
</html>