2

In MySQLi's prepared statement API, bind_param takes a type specification char.

Type specification chars

MySQLi's fetch_field returns an object with several properties related to the field. However, the type specification char is not one of the available properties ($field->type returns an integer corresponding to a constant, not a type specification char).

What is the best way to get the type specification char from a MySQLi field?

FThompson
  • 28,352
  • 13
  • 60
  • 93
  • `mysqli_result::fetch_field()` should return the field's data-type. Is that good enough or do you specifically need one of those letters? I'm not sure how a bind_param argument would tie into fetch_field... – Mike B Jan 17 '13 at 00:29
  • @MikeB My goal is to get specifically the type specification char; I could create my own method, but I feel like this would be something that can be done via the core API. – FThompson Jan 17 '13 at 00:32
  • Does it matter? Type does not map 1:1 with the types that bind_param uses. You will have to create your own conversion array. – datasage Jan 17 '13 at 00:33
  • @MikeB Thanks, but I'll likely use the [MySQLi constants](http://php.net/manual/en/mysqli.constants.php) to avoid any issues should the values change in the future. datasage, thanks, that answers my question of whether I would need to write my own method or whether one existed already. – FThompson Jan 17 '13 at 00:38
  • Just out of curiosity, what reason do you need to have the specific types? PHP is a dynamically typed language, so it really shouldn't matter. – Supericy Jan 17 '13 at 00:39
  • @Supericy I have several tables that have user-defined columns, and I am writing a script to handle input to update these columns with. I could neglect from using prepared statements to simplify the process, but I prefer trying to use them whenever possible. And from my understanding of MySQLi's prepared statements, the type specification char must be relevant to the param being bound. – FThompson Jan 17 '13 at 00:42
  • You could do everything as type `s`. Mysql will automatically convert strings to the column's declared datatype. – Barmar Jan 17 '13 at 00:49
  • @Barmar That's perfect, thanks. If you write it up in an answer (with something brief to more directly address the question too), I'll mark it as accepted. – FThompson Jan 17 '13 at 00:52

1 Answers1

1

Just treat everything as a string, and use type s. The mysql server automatically converts strings to the column's actual datatype when processing queries, e.g. you can write things like set int_col = '32' and it will treat it as set int_col = 32.

There's a little extra overhead, since numeric data won't be transmitted in binary; the server will have to parse them. But unless you're doing a huge number of INSERT or UPDATE statements this will probably be negligible.

Barmar
  • 741,623
  • 53
  • 500
  • 612