简体   繁体   中英

how to display information from a database in a web page without php

for my homework assignment, I need to display this information from my database in a table on a webpage. with all the research I've done, it seems that I need to use php. is there anyway to do this without php and just html? we haven't learned php yet so I'm confused. here is the database:

CREATE TABLE album (
id serial PRIMARY KEY,
name text,
number text,
year text,
artist text,
description text
);

CREATE TABLE label (
    id serial PRIMARY KEY,
    title text,
    title_id integer REFERENCES album (id)
);

INSERT INTO album (name, number, year, artist, description) VALUES ('Reputation','15','2017','Taylor Swift','Reputation is Taylor Swifts sixth studio album');
INSERT INTO label (text, title_id) VALUES (Big Machine Records, 1);
INSERT INTO album (name, number, year, artist, description) VALUES ('Ripcord','13','2016','Keith Urban','Ripcord is Keith Urbans ninth studio album');
INSERT INTO label (text, title_id) VALUES (Capital Records Nashville, 2);

You can use Node, but I will recommend using php. It can be learned easily.

Remember those steps and it will be easy for you:

1)Php code is written inside tags and file must be saved with .php extension. 2)You need to connect to the database, there are multiple methods https://www.w3schools.com/php/php_mysql_connect.asp

<?php
       $servername = "localhost";
        $username = "enterusername";
        $password = "enterdatabasepassword"; 
        $database="enterdatabasename";

        // Create connection
        $con = mysqli_connect($servername, $username, $password,$database);

// Check connection

    if (!$con) {
        die("Connection failed: " . mysqli_connect_error());
    }
    echo "Connected successfully";

//this is a comment. 
//Usually in localhost username is root and password is empty, so you must use

    $username="root";
    $password="";


3) Now you are connected to the database. This is how you get data from the database.

// $sql is just a variable
//$con is the variable that stores database connection. We declared it before.

$sql = mysqli_query($con, "SELECT *  FROM album");
$count = mysqli_num_rows($sql); //mysqli_num_rows counts rows returned from database.
//now we check if database returned more than 0 rows
if($count>0){
//if returned rows >0 we fetch the data
while ($row = mysqli_fetch_array($sql)){


//Now we store each field in variables:

$id=$row['id'];
$name=$row['name'];
$number=$row['number'];
$year=$row['year'];
$artist=$row['artist'];
$description=$row['description'];

//Now we can create table

echo "<table><thead><tr> <td>id</td><td>name</td><td>number</td><td>year</td><td>artist</td><td>description</td></tr></thead>
<tbody>
<tr> <td>$id</td><td>$name</td><td>$number</td><td>$year</td><td>$artist</td><td>$description</td></tr>
</tbody>
</table>";


}


}




//Hope this helped you.

The process for read data from a database is: Presentation -> Language -> Driver -> Database.

Where:

Presentation-> The endpoint where you want show the data, it can be an app, webpage, console etc...

Language-> You need a programming language with an interface for the driver, generally this interface is a library.

Driver-> It's an abstraction layer that allow your library connect to a database.

Database-> your data here.

So you need to use programming to show data on a html page, but if you want only show the data you can use a viewer like this:

http://kripken.github.io/sql.js/GUI/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM