Question

Sendgrid says that the Categories are in the form of String data type as follows:

[
  {
    "email": "john.doe@sendgrid.com",
    "timestamp": 1337966815,
    "category": [
      "newuser",
      "transactional"
    ],
    "event": "open"
  },
  {
    "email": "jane.doe@sendgrid.com",
    "timestamp": 1337966815,
    "category": "olduser"
    "event": "open"
  }
]

So, if someone sends a numerical number , say 11111 for category, sendgrid will treat it as a string and send it back .

I always use numerical values to send and when I get the same value back, I store it in an integer column in my database. For example I will be storing 11111 value in my column category_int and it's data type is int(11).

Since, I am always storing it in an integer column, whenever someone sends category in the form of actual string just like above olduser , I get an error.

My Question:

Am I seeing error because,my category_int column is intended to receive integer values and not string values? But if this is true then, isn't the value that sendgrid is sending , in case of numbers(in my case 11111) is treated as string and perhaps, I should be getting error in this case as well. Where as I get errors only when I send category in the form of some descriptive words like olduseretc.

Please advise.

Was it helpful?

Solution

The problem is that an int(11) column cannot contain a string. MySQL is able to understand that "11111" as a string can be changed to the int 11111 and knows how to convert it. But it doesn't know how to make olduser into an int, so it throws an error.

You could change your data type to varchar, and then that column would be able to handle both 11111 and olduser.

You could also check the value in your code before you pass it to the database, and throw out values that cannot be made into ints.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top