Retrieve data from the database and display it to the page PHP+Mysql+Html (simple example)

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

  2. <html xmlns="http://www.w3.org/1999/xhtml">

  3. <head>

  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

  5. <title>Untitled document</title>

  6. <style type="text/css">

  7. table{

  8. border: 1px solid black;

  9. border-color:#F06;

  10. padding:10px;

  11. border-collapse:collapse;

  12. }

  13. table tr th{

  14. border: 1px solid black;

  15. border-color:#F06;

  16. padding:10px;

  17. border-collapse:collapse;

  18. }

  19. table tr td{

  20. border: 1px solid black;

  21. border-color:#F06;

  22. padding:10px;

  23. border-collapse:collapse;

  24. }

  25. </style>

  26. </head>

  27.  
  28. <body>

  29. <center>

  30. <table>

  31. <caption>List of results</caption>

  32. <tr><th>Number</th><th>Name</th><th>Email</th><th>Results</th><th>Time</th></tr>

  33. <?php

  34. //header("Content-Type:text/html;charset=utf-8");//Set the page character encoding

  35. // constant parameters

  36. define("DB_HOST", 'localhost');

  37. define("DB_USER", 'root');

  38. define("DB_PWD",'123');

  39. define("DB_NAME", 'school');

  40.  
  41. // The first step is to connect to the database

  42. $conn = mysql_connect('localhost','root','123') or die('Database connection failed, error message:'.mysql_error());

  43.  

  44. // The second step is to select the specified database and set the character set

  45. mysql_select_db(DB_NAME,$conn) or die('Database error, error message:'.mysql_error());

  46. mysql_query('SET NAMES UTF8') or die('Character set setting error:'.mysql_error());

  47.  

  48. //Add data

  49. // $query = "INSERT INTO grade(name,email,point,regdate)

  50. // VALUES('nurse','[email protected]',67,NOW())";

  51. // mysql_query($query) or die('New error:'.mysql_error());

  52.  
  53. // change the data

  54. // $query = 'UPDATE grade SET point = 87 WHERE id = 7';

  55. // @mysql_query($query) or die('Modification error:'.mysql_error());

  56.  
  57. //delete data

  58. // $query = 'DELETE FROM grade WHERE id = 7';

  59. // @mysql_query($query) or die('Delete error:'.mysql_error());

  60.  
  61. // Display Data

  62. $query = 'SELECT * FROM grade';

  63. $result = mysql_query($query) or die('SQL statement error:'.mysql_error());

  64.  

  65. // Convert the result set into an array and assign it to $row, if there is data, it is true

  66. while(!!$row = mysql_fetch_array($result)){

  67. echo"<tr><td>".$row["id"]."</td><td>".$row["name"]."</td><td>".$row["email"]."</td><td>".$row["point"]."</td><td>".$row["regdate"]."</td><tr>";

  68. }

  69.  
  70. mysql_close($conn);

  71. ?>

  72. </table>

  73. <a href="#">返回</a>

  74.  
  75. </center>

  76. </body>

  77. </html>

Guess you like

Origin blog.csdn.net/keke795/article/details/113065900