MySQL Query returning empty result

429 Views Asked by At

I have a simple SELECT statement which calls all of the columns in my coupon table by who_added.

code:

func CouponByUserID(c *gin.Context) {
    fmt.Println("CouponByUserID()")
    lCoupon := []model.Coupon{}
    whoAdded := c.PostForm("whoAdded")
    fmt.Println("whoAdded: " + whoAdded)

    var sqlSel = ""
    sqlSel = sqlSel + " SELECT * FROM coupon WHERE who_added = ? "

    fmt.Println("sqlSel")
    fmt.Println(sqlSel)
    _, err := dbmap.Select(&lCoupon, sqlSel, whoAdded)

    if err == nil || lCoupon != nil {
        fmt.Println("got result")
        fmt.Println(lCoupon)
        c.Header("Content-Type", "application/json")
        c.JSON(http.StatusOK, lCoupon)
    } else {
        fmt.Println("coupon not found")
        c.JSON(404, gin.H{"error": "coupon not found"})
    }
}

Coupon Model:

type Coupon struct {
    CouponID          int64   `db:"coupon_id, primarykey, autoincrement"`
    CouponName        string  `db:"coupon_name,omitempty"`
    CouponCode        string  `db:"coupon_code,omitempty"`
    Description       string  `db:"description,omitempty"`
    Status            string  `db:"status,omitempty"`
    CampaignStart     int64   `db:"campaign_start,omitempty"`
    CampaignEnd       int64   `db:"campaign_end,omitempty"`
    SourceType        string  `db:"source_type,omitempty"`
    Preview           string  `db:"preview,omitempty"`
    CouponFormat      string  `db:"coupon_format,omitempty"`
    PreviewType       string  `db:"preview_format,omitempty"`
    Display           string  `db:"display,omitempty"`
    DisplayType       string  `db:"display_format,omitempty"`
    Width             float64 `db:"width,omitempty"`
    Height            float64 `db:"height,omitempty"`
    WhoAdded          int     `db:"who_added,omitempty"`
    WhenAdded         int     `db:"when_added,omitempty"`
    WhoUpdated        int     `db:"who_updated,omitempty"`
    WhenUpdated       int     `db:"when_updated,omitempty"`
    TermsAndCondition string  `db:"terms_and_condition,omitempty"`
    CouponLocation    string  `db:"coupon_location,omitempty"`
    BusID             string  `db:"bus_id,omitempty"`
    ImgCoupon         string  `db:"img_coupon,omitempty"`
}

Result: As you can see here it's empty even if my table has data.

postman

Problem: Data on my table is not showing in the result.

I tried to query the same statement on MySQL workbench and it's work just fine.

Where did I go wrong?

0

There are 0 best solutions below