I want to retrieve the values from a database table and show them in a html table in a page. I already searched for this but I couldn't find the answer, although this surely is something easy (this should be the basics of databases lol). I guess the terms I've searched are misleading. The database table name is tickets, it has 6 fields right now (submission_id, formID, IP, name, email and message) but should have another field called ticket_number. How can I get it to show all the values from the db in a html table like this:
<table border="1">
<tr>
<th>Submission ID</th>
<th>Form ID</th>
<th>IP</th>
<th>Name</th>
<th>E-mail</th>
<th>Message</th>
</tr>
<tr>
<td>123456789</td>
<td>12345</td>
<td>123.555.789</td>
<td>John Johnny</td>
<td>johnny@example.com</td>
<td>This is the message John sent you</td>
</tr>
</table>
And then all the other values below 'john'.
Example taken from W3Schools: PHP Select Data from MySQL
It's a good place to learn from!
it would print the table like this just read line by line so that you can understand it easily..
OOP Style : At first connection with database.
Then :
Here is an easy way to fetch data from a MySQL database using PDO.
Object-Oriented with PHP/5.6.25 and MySQL/5.7.17 using MySQLi [Dynamic]
Learn more about PHP and the MySQLi Library at PHP.net.
First, start a connection to the database. Do this by making all the string variables needed in order to connect, adjust them to fit your environment, then create a new connection object with
new mysqli()
and initialize it with the previously made variables as its parameters. Now, check the connection for errors and display a message whether any were found or not. Like this:Next, make a variable that will hold the query as a string, in this case its a
select
statement with alimit
of 100 records to keep the list small. Then, we can execute it by calling themysqli::query()
function from our connection object. Now, it's time to display some data. Start by opening up a<table>
tag throughecho
, then fetch one row at a time in the form of a numerical array withmysqli::fetch_row()
which can then be displayed with a simple for loop.mysqli::field_count
should be self explanatory. Don't forget to use<td></td>
for each value, and also to open and close each row withecho"<tr>"
andecho"</tr>
. Finally we close the table, and the connection as well withmysqli::close()
.Any feedback would be much appreciated! Good Luck!