Mysql
 SQL >> Datenbank >  >> RDS >> Mysql

MySQL-Zeilen in PHP als HTML-Tabelle ausgeben

Eine vollständige, in sich geschlossene Seite, die verbindet, abfragt und die Ergebnisse in einer Tabelle ausgibt:

 <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>database connections</title> </head> <body> <?php $username = "database-username"; $password = "database-password"; $host = "localhost"; $connector = mysql_connect($host,$username,$password) or die("Unable to connect"); echo "Connections are made successfully::"; $selected = mysql_select_db("test_db", $connector) or die("Unable to connect"); //execute the SQL query and return records $result = mysql_query("SELECT * FROM table_one "); ?> <table border="2" style= "background-color: #84ed86; color: #761a9b; margin: 0 auto;" > <thead> <tr> <th>Employee_id</th> <th>Employee_Name</th> <th>Employee_dob</th> <th>Employee_Adress</th> <th>Employee_dept</th> <td>Employee_salary</td> </tr> </thead> <tbody> <?php while( $row = mysql_fetch_assoc( $result ) ){ echo "<tr> <td>{$row\['employee_id'\]}</td> <td>{$row\['employee_name'\]}</td> <td>{$row\['employee_dob'\]}</td> <td>{$row\['employee_addr'\]}</td> <td>{$row\['employee_dept'\]}</td> <td>{$row\['employee_sal'\]}</td> </tr>\n"; } ?> </tbody> </table> <?php mysql_close($connector); ?> </body> </html> 

Quelle: retrieve data from db and display it in table in php

Strukturell ist das Muster in Ordnung: Kopfzeile einmal, Zeilen im Body in einer Schleife. Vor dem Einsatz sind drei Punkte zu beachten. Die mysql_*-Funktionen existieren in PHP 7+ nicht mehr, die letzte Kopfzelle sollte ein <th> statt eines <td> sein, und jeder in Markup ausgegebene Wert braucht htmlspecialchars(), um keine XSS-Lücke zu öffnen.