<--SyntaxHighlighter--> SyntaxHighlighter.all();

 

package main

import (
	"database/sql"
	"fmt"	

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

func checkError(err error) {
    if err != nil {
        panic(err)
    }
}

func main() {

	var name, email, phone string

	db, err := sql.Open("mysql", "root:1234@tcp(127.0.0.1:3306)/testdb")
	checkError(err)
	defer db.Close()
    
	fmt.Println("connect success")

	rows, err := db.Query("SELECT name, email, phone FROM members where id=?", 6)
	checkError(err)
	defer rows.Close()

	for rows.Next() {
		err := rows.Scan(&name, &email, &phone)
		checkError(err)
		fmt.Println("rows", name, email, phone)
	}
}

+ Recent posts