Current Location: Home> Function Categories> fetch_lengths

fetch_lengths

Returns the column length of the current row in the result set.
Name:fetch_lengths
Category:Uncategorized
Programming Language:php
One-line Description:Returns the column length of the current row in the result set.

Definition and usage

lengths / mysqli_fetch_lengths() function returns the field length of the current row in the result set.

Example

Example 1 - Object-Oriented Style

Returns the field length of the current row in the result set:

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

if ( $mysqli -> connect_errno ) {
  echo "Failed to connect to MySQL: " . $mysqli -> connect_error ;
  exit ( ) ;
}

$sql = "SELECT * FROM Persons ORDER BY Lastname" ;

if ( $result = $mysqli -> query ( $sql ) ) {
  $row = $result -> fetch_row ( ) ;
  // Show field length
  foreach ( $result -> lengths as $i => $val ) {
    printf ( "Field %2d has length: %2d\n" , $i + 1 , $val ) ;
  }
  $result -> free_result ( ) ;
}

$mysqli -> close ( ) ;
?>

Example 2 - Procedural Style

Returns the field length of the current row in the result set:

 <?php
$con = mysqli_connect ( "localhost" , "my_user" , "my_password" , "my_db" ) ;

if ( mysqli_connect_errno ( ) ) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error ( ) ;
  exit ( ) ;
}

$sql = "SELECT * FROM Persons ORDER BY Lastname" ;

if ( $result = mysqli_query ( $con , $sql ) ) {
  $row = mysqli_fetch_row ( $result ) ;
  // Show field length
  foreach ( mysqli_fetch_lengths ( $result ) as $i => $val ) {
    printf ( "Field %2d has length: %2d\n" , $i + 1 , $val ) ;
  }

  mysqli_free_result ( $result ) ;
}

mysqli_close ( $con ) ;
?>
Similar Functions
Popular Articles