if you want to know the setting of the field length you should use 'length' instead of 'max_length'
$q = $mysqli->query("select * from table");
$finfo = $q->fetch_fields();
foreach ($finfo as $val) {
echo $val->length;
}
mysqli_fetch_fields
result->fetch_fields()
(No version information available, might be only in CVS)
result->fetch_fields() — Retorna uma matriz de objetos representando os campos em um conjunto de resultados
Descrição
Estilo de procedimento:
Estilo orientado a objeto (metodo):
Esta função serve para o mesmo propósito que a função mysqli_fetch_field() com uma simples diferença, ao invés de retornar um objeto de cada vez para cada campo, as colunas são retornadas como uma matriz de objetos.
Parâmetros
- result
-
Apenas para estilo de procedimento: Um identificador de um conjunto de resultados retornado por mysqli_query(), mysqli_store_result() ou mysqli_use_result().
Valor Retornado
Retorna uma matriz de objetos o quais contém informações sobre as definições dos campo ou FALSE se não houver inforções dos campos disponíveis.
| Propriedade | Descrição |
|---|---|
| name | O nome da coluna |
| orgname | O nome original da coluna se foi especificado um alias |
| table | O nome da tabela a qual o campo pertence (se não for calculado) |
| orgtable | Nome da tabela original se foi especificado um alias |
| def | O valor padrão para este campo, representado como um string |
| max_length | A largura máxima do campo no conjunto de resultados. |
| length | O tamanho do campo, como específicado na definição da tabela. |
| charsetnr | O número do conjunto de caracteres do campo. |
| flags | Um inteiro representando bit-flags para o campo. |
| type | O tipo de dados usado para este campo |
| decimals | O número de decimais usados (para campos integer) |
Exemplos
Example#1 Estilo orientado a objeto
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT Name, SurfaceArea from Country ORDER BY Code LIMIT 5";
if ($result = $mysqli->query($query)) {
/* Get field information for all columns */
$finfo = $result->fetch_fields();
foreach ($finfo as $val) {
printf("Name: %s\n", $val->name);
printf("Table: %s\n", $val->table);
printf("max. Len: %d\n", $val->max_length);
printf("Flags: %d\n", $val->flags);
printf("Type: %d\n\n", $val->type);
}
$result->close();
}
/* close connection */
$mysqli->close();
?>
Example#2 Estilo de procedimento
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT Name, SurfaceArea from Country ORDER BY Code LIMIT 5";
if ($result = mysqli_query($link, $query)) {
/* Get field information for all columns */
$finfo = mysqli_fetch_fields($result);
foreach ($finfo as $val) {
printf("Name: %s\n", $val->name);
printf("Table: %s\n", $val->table);
printf("max. Len: %d\n", $val->max_length);
printf("Flags: %d\n", $val->flags);
printf("Type: %d\n\n", $val->type);
}
mysqli_free_result($result);
}
/* close connection */
mysqli_close($link);
?>
O exemplo acima irá imprimir:
Name: Name Table: Country max. Len: 11 Flags: 1 Type: 254 Name: SurfaceArea Table: Country max. Len: 10 Flags: 32769 Type: 4
mysqli_fetch_fields
20-Feb-2008 04:27
06-Feb-2008 04:11
def does not return default values for enumeration types.
09-Mar-2007 01:24
"max_length: The maximum width of the field for the result set."
Not a native English speaker, I could be reading this wrong, but the max_length property doesn't reflect the maximum length of a field but returns the current length of the contents of a field (something that can easily be derived using the strlen() function):
mysql> describe domains;
+--------------+--------------+
| Field | Type |
+--------------+--------------+
...
| name | varchar(255) |
...
$info = $query->fetch_fields();
foreach ($info as $value)
print "[max_length] ".$value->max_length."\n";
This results in:
[max_length] 14
The number of characters in this field actually is 14, but the maximum length of the field is 255.
07-Mar-2007 07:12
The "type" property will return a numerical representation of a field type instead of a "meaningful" string.
Here is an array that may help you:
<?php
$mysqli_type = array();
$mysqli_type[0] = "DECIMAL";
$mysqli_type[1] = "TINYINT";
$mysqli_type[2] = "SMALLINT";
$mysqli_type[3] = "INTEGER";
$mysqli_type[4] = "FLOAT";
$mysqli_type[5] = "DOUBLE";
$mysqli_type[7] = "TIMESTAMP";
$mysqli_type[8] = "BIGINT";
$mysqli_type[9] = "MEDIUMINT";
$mysqli_type[10] = "DATE";
$mysqli_type[11] = "TIME";
$mysqli_type[12] = "DATETIME";
$mysqli_type[13] = "YEAR";
$mysqli_type[14] = "DATE";
$mysqli_type[16] = "BIT";
$mysqli_type[246] = "DECIMAL";
$mysqli_type[247] = "ENUM";
$mysqli_type[248] = "SET";
$mysqli_type[249] = "TINYBLOB";
$mysqli_type[250] = "MEDIUMBLOB";
$mysqli_type[251] = "LONGBLOB";
$mysqli_type[252] = "BLOB";
$mysqli_type[253] = "VARCHAR";
$mysqli_type[254] = "CHAR";
$mysqli_type[255] = "GEOMETRY";
?>

mysqli_fetch_field