-2

My MySql database has two columns: name and time. I would like to display each value in a table that has this code:

<tr>
    <td class="tg-yw4l">name</td>
    <td class="tg-yw4l">time</td>
</tr>

I have already made the connection and the appropriate query to display all the values, but I do not know how to insert them into this html code.

<?php

session_start();
require_once "php/connect.php";

$polaczenie = new mysqli($host, $db_user, $db_password, $db_name);

if ($polaczenie->connect_errno != 0) {
    echo "Error: " . $polaczenie->connect_errno;
} else {

@$polaczenie->query("SELECT * FROM ranking");

...

}

$polaczenie->close();
?>
Adrian12
  • 33
  • 4
  • **WARNING**: Using the error-suppressing YOLO operator (`@`) obscures problems with your code and makes debugging issues like this a whole lot more complicated. That's a tool of last resort and should only be used in exceptional circumstances. You should display an error message for the user, log a problem, initiate some kind of retry, or all of these things in conjunction. – tadman Jul 18 '18 at 21:35
  • You'll need a form to capture the values and `INSERT INTO` to insert the data. You'll also need to do form validation to ensure the values are as expected (not blank, not too long or too short, etc.) prior to insertion and a way of displaying any errors on the form itself. Then you need to fetch the records, row by row, and iterate over that section to display it. – tadman Jul 18 '18 at 21:36
  • If you're just getting started with PHP and want to build applications, I'd also strongly recommend looking at various [development frameworks](http://codegeekz.com/best-php-frameworks-for-developers/) to see if you can find one that fits your style and needs. They come in various flavors from lightweight like [Fat-Free Framework](https://fatfreeframework.com/) to far more comprehensive like [Laravel](http://laravel.com/). These give you concrete examples to work from and much stronger guidance on how to write your code and organize your files. – tadman Jul 18 '18 at 21:36
  • I for one don't know what it is you're asking. – Funk Forty Niner Jul 18 '18 at 21:41
  • 1
    Possible duplicate of [Show values from a MySQL database table inside a HTML table on a webpage](https://stackoverflow.com/questions/17902483/show-values-from-a-mysql-database-table-inside-a-html-table-on-a-webpage) – ficuscr Jul 18 '18 at 21:50

2 Answers2

1

if the connection and the answer of database is already ok, you must only read the data and for each line, write (with php) a line of the table:

echo "<table><tr><td>Name</td><td>Time</td></tr>",
foreach($polaczenie as $data) {  
    echo "<tr><td>".$data['name']."</td><td>".$data['time']."</td></tr>";
}
echo "</table>"

this should make a table like this:

------------------
| Name   | Time  |
------------------
| name1  | time1 |
------------------
| name2  | time2 |
------------------
| name3  | time3 |
------------------
| ....   | ....  |
------------------
0

Please take a look at official manual http://php.net/manual/en/pdo.query.php#refsect1-pdo.query-examples

Damian Dziaduch
  • 2,107
  • 1
  • 15
  • 16