READING DATA FROM A DATABASE TABLE VIA PHP AND MYSQL

Details Table

AdmissionNameSectionMediumTransportAmbition
30326PasangiSinhalaBus To be interior designer
30325SanthushiEnglish Personal Vehicle To become a general manager
30327GayanimaEnglish Personal Vehicle CEO
30328Do Min-joonArtSinhalaBus Actor
30329SiripalaArtEnglish Personal Vehicle Actor
30330Kim Jong-unScienceSinhalaBus Personal Vehicle School VanPresident
30331Elon MuskScienceEnglish Personal Vehicle CEO of Tesla Motors
30332AbittiyaArtSinhalaBus To be monk
27004TaviniScienceEnglishBus To be a lawyer
90000pereraCommerceEnglishBus kkmkk
10023IshaniEnglish
30338SiripalaScienceEnglishBus Nothin
34908sawandiCommerceEnglish School Van
52283abcCommerceEnglishBus School Van
20911ScienceSinhalaBus, Personal Vehicle, faerqre2
58352Lee min huuCommerceEnglish, Personal Vehicle, actor
27468Ahn Hyo SeopArtEnglish, Personal Vehicle, Actor,Singer,Model
12345SirisenaCommerceSinhalaBus, , ssfsfg
70990AliceArtSinhalaBus, Personal Vehicle, School VanFashion Designer
13813ABCDEFScienceEnglishBus, , School VanDFFSDSF
14515XYZScienceEnglishBus, Personal Vehicle, CCCC
11990PQRSTCommerceEnglishBus, Personal Vehicle, School Van AAAAAAAAAA
8383hhhhCommerceEnglish, ,
19078D-1ScienceEnglishBus, , aaaa
28535Thisini ArtEnglish, Personal Vehicle,
28534Heshani himayaScienceEnglish, Personal Vehicle,
56789abcdefgScienceSinhalaBus, Personal Vehicle, test test
897287CobraScienceEnglishBus, Personal Vehicle, srtgdhdghse
19175KawindyaScienceEnglishBus, Personal Vehicle, fjzdgfskgfsjhf
1957test-2024CommerceSinhalaBus, Personal Vehicle, School Van afsadgdsfgs
29100Denawaka HamineScienceSinhala, , School VanActress


SQL CREATE TABLE STATEMENT

CREATE TABLE Details
(
  Admission INT,
  Name VARCHAR(150),
  Section VARCHAR(20),
  SubMedium VARCHAR(20),
  Transport VARCHAR(25),
  Ambition VARCHAR(300),
  PRIMARY KEY(Admission)
)


PHP CODE

  1. <?php
  2. $servername = "localhost";
  3. $username = "root";
  4. $password = "";
  5. $dbname = "School";
  6. $conn = new mysqli($servername, $username, $password, $dbname);
  7. $sql = "SELECT * FROM Details";
  8.  
  9. if($conn == false)
  10. {
  11. die("ERROR: Could not connect." .connect_error());
  12. }
  13.  
  14. else
  15. {
  16. if($result = $conn->query($sql))
  17. {
  18. if($result->num_rows > 0)
  19. {
  20. echo "<table border=1>";
  21. echo "<tr>";
  22. echo "<th>Admission</th> <th>Name</th> <th>Section</th> <th>Medium</th> <th>Transport</th> <th>Ambition</th>";
  23. echo "</tr>";
  24.  
  25. while($row =$result->fetch_assoc())
  26. {
  27. echo "<tr>";
  28. echo "<td>" . $row['Admission'] . "</td>";
  29. echo "<td>" . $row['Name'] . "</td>";
  30. echo "<td>" . $row['Section'] . "</td>";
  31. echo "<td>" . $row['SubMedium'] . "</td>";
  32. echo "<td>" . $row['Transport'] . "</td>";
  33. echo "<td>" . $row['Ambition'] . "</td>";
  34. echo "</tr>";
  35. }
  36. echo "</table>";
  37. }
  38.  
  39. else
  40. {
  41. echo "<br>"."No records matching your query were found.";
  42. }
  43.  
  44. }
  45. else
  46. {
  47. echo "<br>"."ERROR: Could not able to execute $sql. " . error($conn);
  48. }
  49. }
  50. mysqli_close($conn);
  51. ?>