Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法连接到XAMPP MySQL_Mysql_Sql_Database_Go_Xampp - Fatal编程技术网

无法连接到XAMPP MySQL

无法连接到XAMPP MySQL,mysql,sql,database,go,xampp,Mysql,Sql,Database,Go,Xampp,我是新来的。 我正在尝试使用MySQL和Go。 我已经安装了包含Apache和MySQL的XAMPP。 我还使用了database/sql包和github.com/go-sql-driver/mysql驱动程序 我想不出这个错误。我使用的是LiteIDE,它没有显示任何错误,但是记录没有插入到我的数据库中 我的代码是: // RESTAPI project main.go package main import ( "database/sql" "encoding/json"

我是新来的。 我正在尝试使用MySQL和Go。 我已经安装了包含Apache和MySQL的XAMPP。 我还使用了database/sql包和github.com/go-sql-driver/mysql驱动程序

我想不出这个错误。我使用的是LiteIDE,它没有显示任何错误,但是记录没有插入到我的数据库中

我的代码是:

// RESTAPI project main.go
package main

import (
    "database/sql"
    "encoding/json"
    "fmt"
    "log"
    "net/http"

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

    "github.com/gorilla/mux"
)

type API struct {
    Message string "json:message"
}
type User struct {
    ID    int    "json:id"
    Name  string "json:username"
    Email string "json:email"
    First string "json:first"
    Last  string "json:last"
}

func CreateUser(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintln("Create file")
    NewUser := User{}
    NewUser.Name = r.FormValue("user")
    NewUser.Email = r.FormValue("email")
    NewUser.First = r.FormValue("first")
    NewUser.Last = r.FormValue("last")
    output, err := json.Marshal(NewUser)
    fmt.Println(string(output))
    if err != nil {
        fmt.Println("Something went wrong!")
    }
    dsn := "root:password@/dbname"
    db, err := sql.Open("mysql", dsn)
    sql := "INSERT INTO users set user_nickname='" + NewUser.Name + "', user_first='" + NewUser.First + "', user_last='" + NewUser.Last + "', user_email='" + NewUser.Email + "'"
    q, err := db.Exec(sql)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(q)
    fmt.Println("User is created")
}

func main() {
    routes := mux.NewRouter()
    routes.HandleFunc("/api/user/create", CreateUser).Methods("GET")
    http.ListenAndServe(":8080", nil)
}

正如Tom提到的,您应该在sql之后检查错误。打开:

    db, err := sql.Open("mysql", dsn)
    if err != nil {
        log.Fatal(err)
    }
注意:不要在SQL中使用字符串连接

要防止SQL注入,应使用预处理语句:

    sql := "INSERT INTO users set user_nickname='?', user_first='?', user_last='?', user_email='?'"
    q, err := db.Exec(sql, NewUser.Name, NewUser.First, NewUser.Last, NewUser.Email)

您确定可以连接到数据库吗?您没有处理DSN连接错误。