如何将值从MySQL传递到Go模板中

huangapple go评论192阅读模式
英文:

How to pass value from MySQL to go template

问题

这是一个用于将值传递给模板的结构体:

type Entry struct {
    Name, Mes string
}

这是一个处理HandleFunc("/",mysqlWithTempl)的函数:

func mysqlWithTempl(w http.ResponseWriter,r *http.Request) {
    
    // 打开数据库
    con,err := sql.Open("mymysql",dbName+"/"+dbUserName+"/"+dbPassword)
    if err != nil {
        panic(err)
    }

    // 关闭数据库
    defer con.Close()

    // 查询
    rows,err := con.Query("select name,message from entry")

    tRes := Entry{}

    // 获取结果
    for rows.Next() {
        var name,message string
        rows.Scan(&name,&message)
        tRes.Name = name
        tRes.Mes = message
    }
    index.Execute(w,tRes)
}

这是一个模板:

<!DOCTYPE html>
<html>
    <head>
        <title> Test </title>
    </head>
    <body>
        <section id="contents">
        <p> {{.Mes}} {{.Name}} </p>
       </section>
    </body>
</html>

我的问题是如何从mysql中获取更多的值并将其传递给模板。

当尝试以下代码时:

for rows.Next() {
    var name,message string
    rows.Scan(&name,&message)
    tRes.Name = name
    tRes.Mes = message
    index.Execute(w,tRes)
}

所有结果都被获取,但每个结果都被重复显示。

英文:

This struct to pass value to template

type Entry struct {
    Name, Mes string
}

This function to handle with HandleFunc(&quot;/&quot;, mysqlWithTempl)

func mysqlWithTempl(w http.ResponseWriter, r *http.Request) {
    
	// Open database
	con, err := sql.Open(&quot;mymysql&quot;, dbName+&quot;/&quot;+dbUserName+&quot;/&quot;+dbPassword)
	if err != nil {
		panic(err)
	}

	// Close database 
	defer con.Close()

	//query
	rows, err := con.Query(&quot;select name, message from entry&quot;)

	tRes := Entry{}

	//fetch result
	for rows.Next() {
		var name, message string
		rows.Scan(&amp;name, &amp;message)
		tRes.Name = name
		tRes.Mes = message
	}
	index.Execute(w, tRes)
}

and this template

&lt;!DOCTYPE html&gt;
&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt; Test &lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;section id=&quot;contents&quot;&gt;
        &lt;p&gt; {{.Mes}} {{.Name}} &lt;/p&gt;
       &lt;/section&gt;
    &lt;/body&gt;
&lt;/html&gt;

my ask how can fetch more value from mysql and pass it to template

When try

 for rows.Next() {
    		var name, message string
    		rows.Scan(&amp;name, &amp;message)
    		tRes.Name = name
    		tRes.Mes = message
index.Execute(w, tRes)
    	}

all result fetched but every thing repeated

答案1

得分: 5

我认为你可能在问如何显示多个结果?

如果是这样,那么你想要将所有的行累积到一个切片中,代码如下:

results := []Entry
for rows.Next() {
    var name, message string
    rows.Scan(&name, &message)
    tRes.Name = name
    tRes.Mes = message
    results = append(results, tRes)
}
index.Execute(w, results)

然后你的模板将会改变如下:

<!DOCTYPE html>
<html>
    <head>
        <title> Test </title>
    </head>
    <body>
        <section id="contents">
        {{range .}}
        <p> {{.Mes}} {{.Name}} </p>
        {{end}}
       </section>
    </body>
</html>

我相信这将导致每行返回的mysql查询结果运行一次<p> {{.Mess}} {{.Name}} </p>

不过我还没有测试过这段代码,所以可能会有错误。

英文:

I think you might be asking how to show multiple results?

If so then you want to accumulate all your rows into a slice like so:

results := []Entry
for rows.Next() {
    var name, message string
    rows.Scan(&amp;name, &amp;message)
    tRes.Name = name
    tRes.Mes = message
    results = append(results, tRes)
}
index.Execute(w, results)

Then your template will change to look like this:

&lt;!DOCTYPE html&gt;
&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt; Test &lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;section id=&quot;contents&quot;&gt;
        {{range .}}
        &lt;p&gt; {{.Mes}} {{.Name}} &lt;/p&gt;
        {{end}}
       &lt;/section&gt;
    &lt;/body&gt;
&lt;/html&gt;

I believe this will cause one &lt;p&gt; {{.Mess}} {{.Name}} &lt;/p&gt; to be run per row that your mysql query returns.

I haven't actually tested this code though so it might have errors.

huangapple
  • 本文由 发表于 2012年9月11日 01:23:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/12356584.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定