首頁 >php教程 >php手册 >PHP查询数据库并分栏显示结果

PHP查询数据库并分栏显示结果

WBOY
WBOY原創
2016-06-21 08:52:371037瀏覽

PHP查询MySQL数据库,并将结果分栏显示,如下代码:

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><span class="tag-name">html</span><span class="tag">></span><span> </span></span></span></li>
<li><span><span class="tag"><span class="tag-name">head</span><span class="tag">></span><span> </span></span></span></li>
<li class="alt"><span><span class="tag"></span><span class="tag-name">php</span><span>  </span></span></li>
<li><span>session_start(); </span></li>
<li class="alt"><span>include("conn.php"); <span class="tag">?></span><span> </span></span></li>
<li><span><span class="tag"><span class="tag-name">title</span><span class="tag">></span><span>查询结果的分栏显示 - www.cxybl.com</span><span class="tag"></span><span class="tag-name">title</span><span class="tag">></span><span> </span></span></span></li>
<li class="alt"><span><span class="tag"></span><span class="tag-name">head</span><span class="tag">></span><span> </span></span></li>
<li><span><span class="tag"><span class="tag-name">body</span><span class="tag">></span><span> </span></span></span></li>
<li class="alt"><span><span class="tag"><span class="tag-name">table</span><span> </span><span class="attribute">width</span><span>=</span><span class="attribute-value">"450"</span><span> </span><span class="attribute">border</span><span>=</span><span class="attribute-value">"1"</span><span> </span><span class="attribute">align</span><span>=</span><span class="attribute-value">"center"</span><span> </span><span class="attribute">cellpadding</span><span>=</span><span class="attribute-value">"0"</span><span> </span><span class="attribute">cellspacing</span><span>=</span><span class="attribute-value">"0"</span><span class="tag">></span><span> </span></span></span></li>
<li><span><span class="tag"></span><span class="tag-name">php</span><span>  for($</span><span class="attribute">i</span><span>=</span><span class="attribute-value">1</span><span>; $i</span><span class="tag"><span>=4; $i++) {  //如果变量</span><span class="tag"><span>=4,按照行进行循环,输出行 </span></span></span></span></li>
<li class="alt"><span>            echo "<span class="tag"><span class="tag-name">tr</span><span class="tag">></span><span>";    </span><span class="tag">?></span><span>   </span></span></span></li>
<li><span>        <span class="tag"></span><span class="tag-name">php</span><span>  if($</span><span class="attribute">i</span><span>==1){ </span></span></li>
<li class="alt"><span>                  $<span class="attribute">query</span><span>=</span><span class="attribute-value">"select * from tb_picture where id order by id desc"</span><span>;  //从数据表mr_lttb中获取表情图          </span></span></li>
<li><span>                  $<span class="attribute">lttb</span><span>=</span><span class="attribute-value">mysql_query</span><span>($query); } </span></span></li>
<li class="alt"><span>                  $<span class="attribute">j</span><span>=</span><span class="attribute-value">1</span><span>;                      //设置变量$</span><span class="attribute">j</span><span>=</span><span class="attribute-value">1</span><span> </span></span></li>
<li><span>                  while ($<span class="attribute">lttb_row</span><span>=</span><span class="attribute-value">mysql_fetch_array</span><span>($lttb)){   //进行while循环输出 </span></span></li>
<li class="alt"><span>                         if ($j<span class="tag"><span>=6)  {   //每行的表情图个数</span><span class="tag"><span>=8,输出如下内容 </span></span></span></span></li>
<li><span>                            if ($<span class="attribute">c</span><span>==0){ $</span><span class="attribute">c</span><span>=</span><span class="attribute-value">"checked"</span><span>; } </span></span></li>
<li class="alt"><span>        <span class="tag">?></span><span> </span></span></li>
<li><span>  <span class="tag"><span class="tag-name">td</span><span> </span><span class="attribute">height</span><span>=</span><span class="attribute-value">"26"</span><span> </span><span class="attribute">align</span><span>=</span><span class="attribute-value">"center"</span><span> </span><span class="attribute">valign</span><span>=</span><span class="attribute-value">"middle"</span><span class="tag">></span><span> </span></span></span></li>
<li class="alt"><span><span class="tag"><span class="tag-name">input</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"xq"</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"radio"</span><span> </span><span class="attribute">value</span><span>=</span><span class="attribute-value">"<?php  echo $lttb_row[id];?>"</span><span class="tag"></span><span class="tag-name">php</span><span> echo $c;</span><span class="tag">?></span><span class="tag">></span><span>           </span></span></span></li>
<li><span><span class="tag"><span class="tag-name">img</span><span> </span><span class="attribute">src</span><span>="</span><span class="tag"></span><span class="tag-name">php</span><span> echo "</span><span class="attribute-value">image_1</span><span>.php?</span><span class="attribute">recid</span><span>=$lttb_row[id]";</span><span class="tag">?></span><span>" </span><span class="attribute">width</span><span>=</span><span class="attribute-value">"16"</span><span> </span><span class="attribute">height</span><span>=</span><span class="attribute-value">"16"</span><span class="tag">></span><span class="tag"></span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></span></li>
<li class="alt"><span>      <span class="tag"></span><span class="tag-name">php</span><span>  } </span></span></li>
<li><span>              $<span class="attribute">c</span><span>=</span><span class="attribute-value">1</span><span>;                                </span></span></li>
<li class="alt"><span>              ++$j;           //当表情图的个数增加时 </span></li>
<li><span>              if($<span class="attribute">j</span><span>==7){  break;  }   //当表情图的个数等于9时,跳转到下一行重新进行循环 </span></span></li>
<li class="alt"><span>                            } </span></li>
<li><span>            echo "<span class="tag"></span><span class="tag-name">tr</span><span class="tag">></span><span>"; </span></span></li>
<li class="alt"><span>        } </span></li>
<li><span><span class="tag">?></span><span> </span></span></li>
<li class="alt"><span><span class="tag"></span><span class="tag-name">table</span><span class="tag">></span><span> </span></span></li>
<li><span><span class="tag"></span><span class="tag-name">body</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span><span class="tag"></span><span class="tag-name">html</span><span class="tag">></span><span> </span></span></li>
</ol>

conn.php:

<ol class="dp-c">
<li class="alt"><span><span><?php   </span></span></span></li>
<li><span><span class="vars">$id</span><span>=mysql_connect(</span><span class="string">"localhost"</span><span>,</span><span class="string">"root"</span><span>,</span><span class="string">"mysql"</span><span>) </span><span class="keyword">or</span><span> </span><span class="keyword">die</span><span>(</span><span class="string">'连接失败:'</span><span> . mysql_error()); </span></span></li>
<li class="alt"><span><span class="keyword">if</span><span>(mysql_select_db(</span><span class="string">"phpjcdb"</span><span>,</span><span class="vars">$id</span><span>))  </span><span class="comment">//说明:phpjcdb 是数据库名称</span><span> </span></span></li>
<li><span>  <span class="func">echo</span><span> </span><span class="string">""</span><span>; </span></span></li>
<li class="alt"><span>  <span class="keyword">else</span><span> </span></span></li>
<li><span>  <span class="func">echo</span><span> (</span><span class="string">'数据库选择失败:'</span><span> . mysql_error()); </span></span></li>
<li class="alt"><span>mysql_query(<span class="string">"set names gb2312"</span><span>);  </span><span class="comment">//设置为简体中文</span><span> </span></span></li>
<li><span>?> </span></li>
</ol>



陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn