繁体   English   中英

如何在没有php的情况下显示网页中数据库的信息

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

对于我的家庭作业,我需要在网页上的表格中显示我的数据库中的这些信息。 经过我所做的所有研究,似乎我需要使用php。 反正有没有PHP和只是HTML? 我们还没有学过php,所以我很困惑。 这是数据库:

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);

你可以使用Node,但我会建议使用php。 它很容易学习。

记住这些步骤,对您来说将很容易:

1)Php代码写在标签内,文件必须以.php扩展名保存。 2)你需要连接数据库,有多种方法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.

从数据库读取数据的过程是:演示->语言->驱动程序->数据库。

哪里:

演示文稿 - >您想要显示数据的端点,它可以是应用程序,网页,控制台等...

语言->您需要一种具有用于驱动程序接口的编程语言,通常该接口是一个库。

驱动程序->这是一个抽象层,允许您的库连接到数据库。

数据库 - >您的数据在这里。

因此,您需要使用编程来在html页面上显示数据,但是如果您只想显示数据,则可以使用如下查看器:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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