V
V
vlarkanov2019-11-05 11:41:34
MySQL
vlarkanov, 2019-11-05 11:41:34

How to use variable as column name?

There is a function that should check if the user has rights. If there is a right - in cell 1, if not - 0.

func checkIfGroupHasRight(db *sql.DB, groupname interface{}, privilege string) bool {
  ...
  rows, err := db.Query("SELECT ? FROM hellgate_groups WHERE groupname=?", privilege, strGroupname)
  ...
}

checkIfGroupHasRight(db, session.Values["usergroup"], "deleteuser")

I get an error when calling
error: "sql: Scan error on column index 0, name \"?\": converting driver.Value type []uint8 (\"deleteuser\") to a int: invalid syntax"

What am I doing wrong? Why is privilege (string) perceived by the driver as []uint8 and why is it trying to cast to int?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lazy @BojackHorseman MySQL, 2019-11-05
@vlarkanov

no way. placeholders in requests only values ​​are passed

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question