Home >Backend Development >PHP Tutorial >PHP mysql database operation example learning

PHP mysql database operation example learning

WBOY
WBOYOriginal
2016-07-25 09:13:131151browse

1, get table data 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. ?>
Copy code

Output result: PHP mysql database operation example learning

2, get table data 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. ?>
Copy the code

output result: PHP mysql database operation example learning

3, Get table data 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. ";
  7. print "
  8. ";
  9. while ($row = mysql_fetch_array($result))
  10. {
  11. print "
  12. ";
  13. print "
  14. ";
  15. print "
  16. ";
  17. }
  18. print "
  19. encoding Name
    {$row[" id"]} {$row["name"]}
    ";
  20. ?>
Copy the code

output result: PHP mysql database operation example learning 4. Get data paging 04

  1. $page=isset($_GET['page'])?intval($_GET['page']):1;
  2. $num=3; //Display 3 per page Piece of data
  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; //Get the value of the first parameter of limit. If the first page is (1-1)*10=0, the second page is (2 -1)*10=10.
  12. $info=mysql_query("select id,name from tb_test limit $offset,$num");
  13. print "";
  14. print "
  15. ";
  16. print "
  17. ";
  18. While($row=mysql_fetch_array($info)){
  19. print "
  20. " ;
  21. print "
  22. ";
  23. print "
  24. ";
  25. print "
  26. ";
  27. }//Display data
  28. print "
  29. Encoding Name
    {$row["id"]} {$row["name"]}
    ";
  30. For($i=1;$i $show=($i!=$page)?"$i":"";
  31. Echo $show." ";
  32. }
  33. ?>
Copy the code

output result: PHP mysql database operation example learning



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn