PHP Doku:: Returns the number of columns for the most recent query - mysqli.field-count.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzDatenbankerweiterungenAnbieterspezifische DatenbankerweiterungenMySQL Improved ExtensionThe MySQLi classmysqli->field_count -- mysqli_field_count

Ein Service von Reinhard Neidl - Webprogrammierung.

The MySQLi class

<<mysqli->error -- mysqli_error

mysqli::get_charset -- mysqli_get_charset>>

mysqli->field_count

mysqli_field_count

(PHP 5)

mysqli->field_count -- mysqli_field_countReturns the number of columns for the most recent query

Beschreibung

Objektorientierter Stil

int $field_count;

Prozeduraler Stil

int mysqli_result::mysqli_field_count ( mysqli $link )

Returns the number of columns for the most recent query on the connection represented by the link parameter. This function can be useful when using the mysqli_store_result() function to determine if the query should have produced a non-empty result set or not without knowing the nature of the query.

Parameter-Liste

link

Nur bei prozeduralem Aufruf: Ein von mysqli_connect() oder mysqli_init() zurückgegebenes Verbindungsobjekt.

Rückgabewerte

An integer representing the number of fields in a result set.

Beispiele

Beispiel #1 mysqli->field_count example

Objektorientierter Stil

<?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("SELECT * FROM friends");

if (
$mysqli->field_count) {
    
/* 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();
?>

Prozeduraler Stil

<?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"SELECT * FROM friends");

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);
?>

5 BenutzerBeiträge:
- Beiträge aktualisieren...
Jonathan
6.03.2007 12:43
Some corrections ;o)

$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";
Typer85 at gmail dot com
2.01.2007 17: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,

?>
dedlfix
18.07.2006 19:26
There are MYSQLI_TYPE_* constants for the type property (listed in http://php.net/manual/en/ref.mysqli.php).

e.g.
<?php
if ($finfo->type == MYSQLI_TYPE_VAR_STRING)
 
// a VARCHAR
jakerosoft at hotmail dot com
16.08.2005 18:15
<?
$fieldinfo
= $result->fetch_field();
if (
$fieldinfo & MYSQLI_NOT_NULL_FLAG)  {
  print
"not null flag is set";
} else {
  print
"not null flag is NOT set";
}
?>
Marc-André
8.07.2005 0:56
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] = "int";
$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[252] = "blob"; // text, blob, tinyblob,mediumblob, etc...
$mysqli_type[253] = "string"; // varchar and char
$mysqli_type[254] = "enum";
?>



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)