Заставить mysql_fetch_assoc автоматически обнаружить типы возвращаемых данных?

Используйте «REPLACE INTO»:

 REPLACE INTO table SET id = 42, foo = 'bar';

См. Больше в документации MySQL

.

7
задан davr 9 June 2009 в 21:05
поделиться

2 ответа

I think a good strategy here is to programatically determine the datatype of each column in a table, and cast the returned results accordingly. This will allow you to interact with your database in a more consistent and simple manner while still giving you the control you need to have your variables storing the correct datatype.

One possible solution: You could use mysql_fetch_field() to get an object that holds meta-data about the table column and then cast your string back to the desired type.

//run query and get field information about the row in the table
$meta = mysql_fetch_field($result, $i);

//get the field type of the current column
$fieldType = $meta->type

A full example can be found here: http://us2.php.net/manual/en/function.mysql-fetch-field.php

Since PHP is loosely typed, you should have a relatively easy time with this.

If you are using OO (object-oriented) techniques, you could create a class with this functionality in the setter() methods so you don't have to have duplicate code.

14
ответ дан 6 December 2019 в 14:09
поделиться

You could build a mysql-specific layer around mdb2 that automatically detects field types using the SHOW COLUMNS command, but that would kind of defeat the purpose of using mdb2.

Keep in mind, also, that mysql suports integers well outside of PHP's range, (UNSIGNED BIGINT is 64 bits; PHP supports, at best, 64 bit signed ints, and less on 32 bit platforms) so automatically casting may be undesirable in some contexts. In those cases, you really want to keep the large ints in their string form, and manipulate them with bcmath

0
ответ дан 6 December 2019 в 14:09
поделиться
Другие вопросы по тегам:

Похожие вопросы: