0

I have a table of thousands of lines of code in which I have to load some data coming from a database in some of the "td" of the table ... Wandering through SO I found several examples of uploads but all of them needed a rewrite of the whole table ... So I ask you, is there a simpler way to pass the values ​​of a database within the table?

In simple words with a SELECT I have to be able to load data inside "td" that contain textBox. Without the need to rewrite the table (given that there are thousands of lines) ...

I am attaching the examples I found on SO:

1° : Load data from MySQL database to HTML textboxes on button click

1
  • Can't really understand much and as you don't have any code example then what you except us to do? Commented Oct 2, 2018 at 9:44

2 Answers 2

0

To load data from MySQL/MariaDB database to table you can use this code:

<!DOCTYPE html>
<?php 
//This is the connection
$mysqli = new mysqli('localhost', 'root', 'DatabasePassword', 'Database Name'); ?>
<html>
<head>
    <title></title>
</head>
<body>
    <div class="content">
        <table class='table table-bordered table-striped'>
        <thead>
            <tr>
            <th>Name</th>
            <th>Address</th>
        </tr>
        </thead>
        <tbody>
            <tr>
                <?php while ($row = $mysqli->fetch_array(MYSQLI_ASSOC)):  ?>
                    <?php foreach ($row as $key => $value): ?>
                    <td alt="<?=$key?>"><?=$value?></td>
                    <?php endforeach; ?>
                <?php endwhile; ?>
            </tr>
        </tbody>
    </table>
    </div>

</body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

0

You want to fill a <table> with database datas?

1) Build your sql query.

2) Execute and retrieve datas.

3) Inject datas into <table> using a foreach.

<?php
    //Connection to db
    $db = new mysqli('127.0.0.1', 'login', 'pass', 'database');

    //Query
    $sql = "SELECT name, surname FROM example";

    //Execution + error checking
    if(!$result = $db->query($sql)){
        die('There was an error running the query [' . $db->error . ']');
    }
?>
<table>
    <thead>
        <tr>
            <th>Name</th>
            <th>Surname</th>
        </tr>
    </thead>
    <tbody>
        <?php
            while($row = $result->fetch_assoc()){
                //Displaying results in table rows
                echo "<tr>
                        <td>".$row["name"]."</td>
                        <td>".$row["surname"]."</td>
                </tr>";
            }
        ?>
    </tbody>
</table>

2 Comments

Please don't recommend using (even as an example) the old, insecure and deprecated mysql_*-functions. They have even been completely removed in PHP 7.
Yes sorry, I did a copy/paste of an existing project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.