web - How to fetch multiple column from mysql using go lang -


i trying fetch multiple columns in mysql database using go language. modify script, , work fetching 1 column, , printing using http print function. however, when fetches 2 things script no longer works. don't have idea need fix it. know sql fine have tested out in mysql terminal, , has given me expected result wanted.

 conn, err := sql.open("mysql", "user:password@tcp(localhost:3306)/database")  statement, err := conn.prepare("select first,second table")   rows, err := statement.query()  rows.next() {          var first string          rows.scan(&first)          var second string          rows.scan(&second)          fmt.fprintf(w, "title of first :"+first+"the second is"+second)  }  conn.close() 

you using wrong variable names assume that's "typo" in sample code.

then correct syntax is:

var first, second string rows.scan(&first, &second) 

see this on scan(dest ...interface{}) means , how use it.

you should handle , not ignore errors.

finally, should use fprintf it's intended:

fmt.fprintf(w, "title of first : %s\nthe second is: %s", first, second) 

Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -