我无法在 HTML 上查看所有 MySQL 表数据

我从MySQL表中获取了所有数据,并使用“log”函数打印了它,但我需要在HTML页面上打印它。我只能打印一列。我想打印所有产品数据。我该怎么做?


简而言之:我想列出商店页面的所有产品,但我只能列出一种产品。(我将添加数百种产品。目前,我的表中只有3种产品。


我已经2天没有解决了它,如果你能帮忙,我会很高兴。非常感谢...转到文件


package main


import ( "database/sql" "fmt" "log" "net/http" "text/template"


_ "github.com/go-sql-driver/mysql"

)


type product struct { ID int Name string Stock int Price float32 }


var products product


func handler(w http.ResponseWriter, r *http.Request) {


products = product{

    ID:    products.ID,

    Name:  products.Name,

    Stock: products.Stock,

    Price: products.Price,

}


appointment, _ := template.ParseFiles("ot.html")

appointment.Execute(w, products)

}


func main() { fmt.Println("Go MySQL Tutorial")


sqlType := "mysql"

sqlUsn := "root"

sqlPass := "2582064"

sqlDatabase := "gowdb"


db, err := sql.Open(sqlType, sqlUsn+":"+sqlPass+"@/"+sqlDatabase)

if err != nil {

    panic(err.Error())

}


defer db.Close()


results, err := db.Query("SELECT * FROM products")

if err != nil {

    panic(err.Error())

}


for results.Next() {


    err = results.Scan(&products.ID, &products.Stock, &products.Price, &products.Name)

    if err != nil {

        panic(err.Error())

    }


    log.Println(products.Name)

}


if err != nil {

    panic(err.Error())

}


http.HandleFunc("/", handler)

http.ListenAndServe(":8080", nil)

}

网页


<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

</head>

<body>

    <h1>PRODUCT 1</h1>

    <div>NAME: {{.Name}}</div>

    <div>PRICE: {{.Price}}</div>

    <div>STOCK: {{.Stock}}</div>

    <div>ID: {{.ID}}</div>

    <div>BUY FROM: xxx.com</div>


<h1>PRODUCT 2</h1>

<div>NAME: {{.Name}}</div>

<div>PRICE: {{.Price}}</div>

<div>STOCK: {{.Stock}}</div>


</body>

</html>

日志上的输出(没关系)


电子掩模丝状面罩气滴面罩


网页上的输出


产品1名称:空投面具价格:19库存:2 ID:2 从中购买:xxx.com


产品2名称:空投面具价格:19库存:2 ID:2 从中购买:xxx.com


产品3名称:空投面具价格:19库存:2 ID:2 从中购买:xxx.com


长风秋雁
浏览 128回答 1
1回答

慕容708150

所以我已经修复了你的代码,但对它持怀疑态度,因为我没有测试它。请记住,原始代码中的一些事情是完全错误的。仅举一例,处理程序从全局变量中获取响应。这是灾难的秘诀,你需要“不习惯”才能做到这一点,否则它会给你带来麻烦。我能说的最好的是让你的老师审查这个代码。main.gopackage mainimport (&nbsp; &nbsp; "database/sql"&nbsp; &nbsp; "net/http"&nbsp; &nbsp; "text/template"&nbsp; &nbsp; _ "github.com/go-sql-driver/mysql")type product struct {&nbsp; &nbsp; ID&nbsp; &nbsp; int&nbsp; &nbsp; Name&nbsp; string&nbsp; &nbsp; Stock int&nbsp; &nbsp; Price float32}func handler(w http.ResponseWriter, r *http.Request) {&nbsp; &nbsp; appointment, _ := template.ParseFiles("ot.html")&nbsp; &nbsp; appointment.Execute(w, GetProducts())}func main() {&nbsp; &nbsp; http.HandleFunc("/", handler)&nbsp; &nbsp; http.ListenAndServe(":8080", nil)}func GetProducts() []product {&nbsp; &nbsp; var out = make([]product, 0, 10)&nbsp; &nbsp; sqlType := "mysql"&nbsp; &nbsp; sqlUsn := "root"&nbsp; &nbsp; sqlPass := "2582064"&nbsp; &nbsp; sqlDatabase := "gowdb"&nbsp; &nbsp; db, err := sql.Open(sqlType, sqlUsn+":"+sqlPass+"@/"+sqlDatabase)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err.Error())&nbsp; &nbsp; }&nbsp; &nbsp; defer db.Close()&nbsp; &nbsp; results, err := db.Query("SELECT * FROM products")&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err.Error())&nbsp; &nbsp; }&nbsp; &nbsp; for results.Next() {&nbsp; &nbsp; &nbsp; &nbsp; var p product&nbsp; &nbsp; &nbsp; &nbsp; err = results.Scan(&p.ID, &p.Stock, &p.Price, &p.Name)&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; panic(err.Error())&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; out = append(out, p)&nbsp; &nbsp; }&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err.Error())&nbsp; &nbsp; }&nbsp; &nbsp; return out}断续器<!DOCTYPE html><html><head>&nbsp; &nbsp; <meta charset="UTF-8">&nbsp; &nbsp; <meta http-equiv="X-UA-Compatible" content="IE=edge">&nbsp; &nbsp; <meta name="viewport" content="width=device-width, initial-scale=1.0">&nbsp; &nbsp; <title>Document</title></head><body>&nbsp; &nbsp; {{ range $i, $e := . }}&nbsp; &nbsp; &nbsp; &nbsp; <h1>PRODUCT $i</h1>&nbsp; &nbsp; &nbsp; &nbsp; <div>NAME: {{.Name}}</div>&nbsp; &nbsp; &nbsp; &nbsp; <div>PRICE: {{.Price}}</div>&nbsp; &nbsp; &nbsp; &nbsp; <div>STOCK: {{.Stock}}</div>&nbsp; &nbsp; &nbsp; &nbsp; <div>ID: {{.ID}}</div>&nbsp; &nbsp; &nbsp; &nbsp; <div>BUY FROM: xxx.com</div>&nbsp; &nbsp; {{end}}</body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go