PHP mysql資料庫操作執行個體學習

來源:互聯網
上載者:User

1,擷取表資料01

  1. mysql_connect("localhost","dev","mysql");
  2. mysql_select_db("dev");
  3. $result=mysql_query("select id,name from tb_test");
  4. while($row=mysql_fetch_array($result,MYSQL_ASSOC)){
  5. print_r($row);echo "
    \n";
  6. }
  7. ?>
複製代碼

輸出結果:

2,擷取表資料02

  1. mysql_connect("localhost","dev","mysql");
  2. mysql_select_db("dev");
  3. $result=mysql_query("select id,name from tb_test");
  4. while($row=mysql_fetch_object($result)){
  5. echo "ID:" .$row->id." text: ".$row->name."
    \n";
  6. }
  7. ?>
複製代碼

輸出結果:

3,擷取表資料03

  1. mysql_connect("localhost","dev","mysql");
  2. mysql_select_db("dev");
  3. $result=mysql_query("select id,name from tb_test");
  4. print "
  5. print "
  6. print "
  7. print "
  8. print "
  9. }
  10. "; "; ";
  11. while ($row = mysql_fetch_array($result))
  12. {
  13. print "
  14. "; "; ";
  15. print "
  16. 編碼名字
    {$row["id"]}{$row["name"]}
    ";
  17. ?>
複製代碼

輸出結果: 4,擷取資料分頁04

  1. $page=isset($_GET['page'])?intval($_GET['page']):1;
  2. $num=3; //每頁顯示3條資料
  3. $db=mysql_connect("localhost","dev","mysql");
  4. $select=mysql_select_db("dev",$db);
  5. $total=mysql_num_rows(mysql_query("select id,name from tb_test"));
  6. $pagenum=ceil($total/$num);
  7. If($page>$pagenum || $page == 0){
  8. Echo "Error : Can Not Found The page .";
  9. Exit;
  10. }
  11. $offset=($page-1)*$num; //擷取limit的第一個參數的值,假如第一頁則為(1-1)*10=0,第二頁為(2-1)*10=10。
  12. $info=mysql_query("select id,name from tb_test limit $offset,$num");
  13. print "
  14. print "
  15. print "
  16. While($row=mysql_fetch_array($info)){
  17. print "
  18. print "
  19. print "
  20. }//顯示資料
  21. "; "; ";
  22. print "
  23. "; "; "; ";
  24. print "
  25. 編碼名字
    {$row["id"]}{$row["name"]}
    ";
  26. For($i=1;$i<=$pagenum;$i++){
  27. $show=($i!=$page)?"$i":"$i";
  28. Echo $show." ";
  29. }
  30. ?>
複製代碼

輸出結果:

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.