<div class="title_bot"><script>ec(2);</script></div> <ol> <li class="alt"><span><span><?php </span></span></span></li> <li> <span>dd = </span><span class="keyword"><strong><font color="#0000ff">new</font></strong></span><span> LampDb(</span><span class="string"><font color="#0000ff">'localhost'</font></span><span>,</span><span class="string"><font color="#0000ff">'root'</font></span><span>,</span><span class="string"><font color="#0000ff">'2184237'</font></span><span>,</span><span class="string"><font color="#0000ff">'note'</font></span><span>,</span><span class="string"><font color="#0000ff">'utf8'</font></span><span>); </span> </li> <li class="alt"> <span class="vars"><font color="#dd0000">$sql</font></span><span> = </span><span class="string"><font color="#0000ff">"select * from mynote"</font></span><span>; </span> </li> <li> <span class="vars"><font color="#dd0000">$handle</font></span><span> = </span><span class="vars"><font color="#dd0000">$dd</font></span><span>->query(</span><span class="vars"><font color="#dd0000">$sql</font></span><span>); </span> </li> <li class="alt"> <span class="vars"><font color="#dd0000">$aa</font></span><span> = </span><span class="vars"><font color="#dd0000">$dd</font></span><span>->fetchOneRow(</span><span class="vars"><font color="#dd0000">$handle</font></span><span>); </span> </li> <li> <span>print_r(</span><span class="vars"><font color="#dd0000">$aa</font></span><span>); </span> </li> <li class="alt"> <span class="func">echo</span><span> </span><span class="string"><font color="#0000ff">"<hr>"</font></span><span>; </span> </li> <li> <span class="vars"><font color="#dd0000">$ddd</font></span><span> = </span><span class="vars"><font color="#dd0000">$dd</font></span><span>->fetchRows(</span><span class="vars"><font color="#dd0000">$handle</font></span><span>); </span> </li> <li class="alt"> <span class="keyword"><strong><font color="#0000ff">for</font></strong></span><span> (</span><span class="vars"><font color="#dd0000">$n</font></span><span>=0; </span><span class="vars"><font color="#dd0000">$n</font></span><span> <span class="func">count</span><span>(</span><span class="vars"><font color="#dd0000">$ddd</font></span><span>); </span><span class="vars"><font color="#dd0000">$n</font></span><span>++){ </span></span> </li> <li> <span class="func">echo</span><span> </span><span class="vars"><font color="#dd0000">$ddd</font></span><span>[</span><span class="vars"><font color="#dd0000">$n</font></span><span>][</span><span class="string"><font color="#0000ff">'id'</font></span><span>] . </span><span class="string"><font color="#0000ff">"标题"</font></span><span> . </span><span class="vars"><font color="#dd0000">$ddd</font></span><span>[</span><span class="vars"><font color="#dd0000">$n</font></span><span>][</span><span class="string"><font color="#0000ff">'subject'</font></span><span>]; </span> </li> <li class="alt"> <span class="func">echo</span><span> </span><span class="string"><font color="#0000ff">"<br>"</font></span><span>; </span> </li> <li><span>} </span></li> <li class="alt"> <span class="func">echo</span><span> </span><span class="string"><font color="#0000ff">"<hr>"</font></span><span>; </span> </li> <li> <span class="func">echo</span><span> </span><span class="string"><font color="#0000ff">"nums="</font></span><span> . </span><span class="vars"><font color="#dd0000">$dd</font></span><span>->fetchRowNums(</span><span class="vars"><font color="#dd0000">$handle</font></span><span>); </span> </li> <li class="alt"><span>?> </span></li> <li> </li> <li>php </li> <li> <span class="keyword">class</span><span> LampDb </span> </li> <li class="alt"><span>{ </span></li> <li> <span class="keyword">private</span><span> </span><span class="vars">$conn</span><span>; </span> </li> <li class="alt"> <span class="comment">//连接数据库 </span><span> </span> </li> <li> <span class="keyword">function</span><span> __construct(</span><span class="vars">$host</span><span>,</span><span class="vars">$user</span><span>,</span><span class="vars">$pass</span><span>,</span><span class="vars">$dbname</span><span>,</span><span class="vars">$charset</span><span>) </span> </li> <li class="alt"><span>{ </span></li> <li> <span> </span><span class="vars">$this</span><span>->conn = mysql_connect(</span><span class="vars">$host</span><span>,</span><span class="vars">$user</span><span>,</span><span class="vars">$pass</span><span>); </span> </li> <li class="alt"> <span> mysql_query(</span><span class="string">'set names $charset'</span><span>); </span> </li> <li> <span> </span><span class="vars">$db</span><span> = mysql_select_db(</span><span class="vars">$dbname</span><span>,</span><span class="vars">$this</span><span>->conn); </span> </li> <li class="alt"><span>} </span></li> <li> <span class="comment">//查询语句 </span><span> </span> </li> <li class="alt"> <span class="keyword">function</span><span> query(</span><span class="vars">$sql</span><span>) </span> </li> <li><span>{ </span></li> <li class="alt"> <span> </span><span class="vars">$handle</span><span> = mysql_query(</span><span class="vars">$sql</span><span>,</span><span class="vars">$this</span><span>->conn); </span> </li> <li> <span> </span><span class="keyword">return</span><span> </span><span class="vars">$handle</span><span>; </span> </li> <li class="alt"><span>} </span></li> <li> <span class="comment">//查询一条记录 </span><span> </span> </li> <li class="alt"> <span class="keyword">function</span><span> fetchOneRow(</span><span class="vars">$handle</span><span>) </span> </li> <li><span>{ </span></li> <li class="alt"> <span> </span><span class="vars">$aa</span><span> = mysql_fetch_assoc(</span><span class="vars">$handle</span><span>); </span> </li> <li> <span> </span><span class="keyword">return</span><span> </span><span class="vars">$aa</span><span>; </span> </li> <li class="alt"><span>} </span></li> <li> <span class="comment">//查询多条记录 </span><span> </span> </li> <li class="alt"> <span class="keyword">function</span><span> fetchRows(</span><span class="vars">$handle</span><span>,</span><span class="vars">$i</span><span>=0) </span> </li> <li><span>{ </span></li> <li class="alt"> <span> </span><span class="keyword">while</span><span>(</span><span class="vars">$bb</span><span> = mysql_fetch_assoc(</span><span class="vars">$handle</span><span>)){ </span> </li> <li> <span> </span><span class="vars">$bbb</span><span>[</span><span class="vars">$i</span><span>] = </span><span class="vars">$bb</span><span>; </span> </li> <li class="alt"> <span> </span><span class="vars">$i</span><span>++; </span> </li> <li><span> } </span></li> <li class="alt"> <span> </span><span class="keyword">return</span><span> </span><span class="vars">$bbb</span><span>; </span> </li> <li><span>} </span></li> <li class="alt"> <span class="comment">//获取一个查询语句返回的记录数 </span><span> </span> </li> <li> <span class="keyword">function</span><span> fetchRowNums(</span><span class="vars">$handle</span><span>) </span> </li> <li class="alt"><span>{ </span></li> <li> <span> </span><span class="comment">//$handle = $this->query($sql); </span><span> </span> </li> <li class="alt"> <span> </span><span class="vars">$num</span><span> = mysql_num_rows(</span><span class="vars">$handle</span><span>); </span> </li> <li> <span> </span><span class="keyword">return</span><span> </span><span class="vars">$num</span><span>; </span> </li> <li class="alt"><span>} </span></li> <li><span>} </span></li> <li class="alt"><span>?> </span></li> </ol>