0

my task at he moment is to show the data from a sql db in a browser. My code is fine for that, but now i have more and bigger sql tables and i am looking for a way to read and show it more dynamic. So that i dont have to write all the names of the column (the echo statements, if i have 100 columns it would not really work like this) in the code. Is there ab better way in this case?

Thanks.

<?php 
            error_reporting(E_ALL);

            $db_link = mysqli_connect (MYSQL_HOST, 
                           MYSQL_BENUTZER, 
                           MYSQL_KENNWORT, 
                           MYSQL_DATENBANK);

            if ( $db_link )
            {
                echo('<p> Verbindung vorhanden: </p>');
            }
            else
            {
                    echo('<p style="color:red"> Keine Verbindung vorhanden: </p>');
            }   
            
            $db_res = mysqli_query($db_link, "SELECT AA, B1, C3 FROM tableXYZ") 
            or die("Fehler: " . mysqli_error());

            echo("<table>");            
            while($row = mysqli_fetch_array($db_res))
            {
                echo("<tr>");
                echo("<td>" . $row["AA"] . "</td>");
                echo("<td>" . $row["B1"] . "</td>");
                echo("<td>" . $row["C3"] . "</td>");
                echo("</tr>");
            }
            echo("</table>");

            ?>
hahakomisch
  • 49
  • 1
  • 8
  • Do you have any specific question about this? Is this an SQL problem, a PHP problem, or an HTML problem? – Nico Haase Feb 10 '22 at 10:13
  • i think it is a php Problem. If i select all columns from the table, then i have to write all the names in a echo statement. if i have e.g. 150 columns, it would not be very good to do it that way. so i am looking for a way to create the table more automatic. – hahakomisch Feb 10 '22 at 10:35
  • "I think" sounds like you should start checking for this. "If i select all columns from the table, then i have to write all the names in a echo statement" - do you? `foreach` can loop over keys and arrays. Also, a table with 150 columns sounds like horrible database design – Nico Haase Feb 10 '22 at 10:51

1 Answers1

0

You could use a foreach loop to show all columns. Like this:

        while($row = mysqli_fetch_array($db_res))
        {
            echo("<tr>");
            foreach ($row as $column) 
            {
                echo("<td>" . $column . "</td>");
            }
            echo("</tr>");
        }

If this is not what you want you have to clarify, in your question, what you really want.

KIKO Software
  • 15,283
  • 3
  • 18
  • 33