mysqli_field_seek> <mysqli_fetch
Last updated: Mon, 28 Dec 2009

mysqli_field_count

mysqli->field_count

(PHP 5)

mysqli->field_count — Retorna o número de colunas para a consulta mais recente

Descrição

Estilo de procedimento:

int mysqli_field_count ( object $link )

Estilo orientado a objeto (metodo):

mysql
int field_count ( void )

Retorna o número de colunas para a consulta mais recente na conexão representada pelo parâmetro link . Esta função pode ser útil ao usar a função mysqli_store_result() para determinar se a consulta produziu um conjunto de resultados vazio ou não sem conhecer a natureza da consulta.

Valores de retorno

Um inteiro representando o número de campos no conjunto de resultados

Exemplo

Example#1 Estilo orientado a objeto

<?php
$mysqli 
= new mysqli("localhost""my_user""my_password""test");

$mysqli->query"DROP TABLE IF EXISTS friends"); 
$mysqli->query"CREATE TABLE friends (id int, name varchar(20))"); 
 
$mysqli->query"INSERT INTO friends VALUES (1,'Hartmut'), (2, 'Ulf')");


$mysqli->real_query($HTTP_POST_VARS['query']);

if (
mysqli_field_count($link)) {
    
/* this was a select/show or describe query */
    
$result $mysqli->store_result();
    
    
/* process resultset */
    
$row $result->fetch_row();

    
/* free resultset */
    
$result->close();
}

/* close connection */
$mysqli->close();
?>

Example#2 Estilo de procedimento

<?php
$link 
mysqli_connect("localhost""my_user""my_password""test");

mysqli_query($link"DROP TABLE IF EXISTS friends"); 
mysqli_query($link"CREATE TABLE friends (id int, name varchar(20))"); 
 
mysqli_query($link"INSERT INTO friends VALUES (1,'Hartmut'), (2, 'Ulf')");

mysqli_real_query($link$HTTP_POST_VARS['query']);

if (
mysqli_field_count($link)) {
    
/* this was a select/show or describe query */
    
$result mysqli_store_result($link);
    
    
/* process resultset */
    
$row mysqli_fetch_row($result);

    
/* free resultset */
    
mysqli_free_result($result);
}

/* close connection */
mysqli_close($link);
?>



User Contributed Notes
mysqli_field_count
Typer85 at gmail dot com
02-Jan-2007 02:33
For those interested and to clarify the Manual Entry.

For query statements that are DESIGNED to return a result set of some sort, this function will always return the number of fields in the table that was queried.

I said DESIGNED because the return value has no effect on whether or not the actual query matched any rows or not.

For example, say I have a table that has 2 fields and only 10 rows. I issue the following query:

<?php

// Assume Connection Blah Blah.

mysqli_query( $connObject , "Select * From `table` Where `Id` > 1000");

// Get Number Of Fields.

mysqli_field_count( $connObject );

// Will Return 2 --> The Number of fields in the table!

?>

It is quite clear that the query itself will never return a result set because I asked it to return rows which have an Id over 1000 and there are only 10 rows.

But because the nature of the query itself is to return a result set, the field count is always returned no matter what.

In contrast, if the query does anything that does not return a result set by nature, such as an insert or update, the field count will always be 0.

Hence, you can easily determine the nature of this query dynamically using these return values.

Good Luck,

?>

mysqli_field_seek> <mysqli_fetch
Last updated: Mon, 28 Dec 2009