mysqli_fetch_lengths() : field length

<?Php
require "config.php";// Database connection file.

$query="select * from student where id=7 ";
if ($result_set = mysqli_query($connection,$query)) {
	 $row = mysqli_fetch_row($result_set);
echo $row[1]."<br>";
foreach (mysqli_fetch_lengths($result_set) as $i => $val) {
	echo " Field No ". $i ." value: ".$val . ",";
  }   
}
?>
Krish Star
Field No 0 value: 1, Field No 1 value: 10, Field No 2 value: 4, 
Field No 3 value: 2, Field No 4 value: 6,
$result_set->lengths;
mysqli_fetch_lengths
The input parameter $result_set is the output of result set identifier of mysqli_query(), mysqli_store_result() or mysqli_use_result().

The output is an array containing legth of all columns of result set row .

MySQLI database connection file

MySQL SELECT Query

Procedural style
<?Php
require "config.php";// Database connection file.
$q="SELECT * FROM student where id=4";
if ($result = $connection->query($q)){
    
$row = $result->fetch_row();

echo $row[1]."<br>";
  foreach ($result->lengths as $i => $val) {
   echo " Field No ". $i ." value: ".$val . ",";
  }
	
 $result->close();
}
?>

Getting field length of all records

<?Php
require "config.php";// Database connection,returns the object $connection
$query='SELECT * FROM student';
if ($result = $connection->query($query)) {
    while ($row = $result->fetch_assoc()) {
  echo "<br><br><b>".$row['id'],$row['name'],$row['class'].$row['mark']."</b>";
      foreach ($result->lengths as $i => $val) {
        printf("<br>Field %2d has Length %2d\n", $i+1, $val);
      }
  }
}
?>
<?Php
require "config.php";// Database connection,returns the object $connection
$query='SELECT * FROM student';
if ($result = mysqli_query($connection, $query)) {
while ($row = mysqli_fetch_assoc($result)) {
  echo "<br><br><b>".$row['id'],$row['name'],$row['class'].$row['mark']."</b>";
    /* display column lengths */
    foreach (mysqli_fetch_lengths($result) as $i => $val) {
      printf("<br>Field %2d has Length %2d\n", $i+1, $val);
    }
}
}
?>
output ( sample rows only )
1John DeoFour75
Field 1 has Length 1
Field 2 has Length 8
Field 3 has Length 4
Field 4 has Length 2
Field 5 has Length 6

2Max RuinThree85
Field 1 has Length 1
Field 2 has Length 8
Field 3 has Length 5
Field 4 has Length 2
Field 5 has Length 4

3ArnoldThree55
Field 1 has Length 1
Field 2 has Length 6
Field 3 has Length 5
Field 4 has Length 2
Field 5 has Length 4
------
------

MySQL DUMP of student table

MYSQLI Functions mysqli_num_rows() Number of rows in result set SELECT query UPDATE query mysqli_fetch_field_direct(): Field meta data
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer