PHP – MySQL – Display Table Content
Simple
Statement:
SELECT column_name(s)
FROM table_name
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <?php // Create connection // Statement: mysqli_connect(host,username,password,dbname) // NOTICE: se lo script è installato nello stesso server del Data Base, host->localhost $con =mysqli_connect( "localhost" , "lucedigi_user" , "mypassword" , "lucedigi_testphp" ); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } // SELECT asterisco (tutti i dati) dalla tabella Persons - START // inserisco i dati nella variabile $result $result = mysqli_query( $con , "SELECT * FROM Persons" ); // Restituisce il set di record come un array // ad ogni chiamata viene restituita la riga successiva while ( $row = mysqli_fetch_array( $result )) { // Visualizza a video i dati echo $row [ 'FirstName' ] . " " . $row [ 'LastName' ]; echo "<br>" ; } // SELECT asterisco (tutti i dati) dalla tabella Persons - END mysqli_close( $con ); echo "Great! Connection Closed!" ; ?> |
Display the Result in an HTML Table
Statement:
SELECT column_name(s)
FROM table_name
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | <?php // Create connection // Statement: mysqli_connect(host,username,password,dbname) // NOTICE: se lo script è installato nello stesso server del Data Base, host->localhost $con =mysqli_connect( "localhost" , "lucedigi_user" , "mypassword" , "lucedigi_testphp" ); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } // Display the Result in an HTML Table - START // SELECT asterisco (tutti i dati) dalla tabella Persons // inserisco i dati nella variabile $result $result = mysqli_query( $con , "SELECT * FROM Persons" ); echo "<table border= '1' > <tr> <th>Firstname</th> <th>Lastname</th> </tr>"; while ( $row = mysqli_fetch_array( $result )) { echo "<tr>" ; echo "<td>" . $row [ 'FirstName' ] . "</td>" ; echo "<td>" . $row [ 'LastName' ] . "</td>" ; echo "</tr>" ; } echo "</table>" ; // Display the Result in an HTML Table - END mysqli_close( $con ); echo "Great! Connection Closed!" ; ?> |
PhpMyAdmin:
Result: