PHP Tutorial





Español Français 中文 Deutsch Portuguese Japanese nederlands
   
 
PHP Topics
Introduction Introduction
Syntax Syntax
Data Types Data Types
Operators Operators
Control Structures Control Structures
Functions Functions
Pre-defined Function Pre-defined Function
Calendar Functions Calendar Functions
Date and Time Date and Time
Array Functions Array Functions
Array List Array Functions List1
Array Function List Array Functions List2
Math Functions Math Functions
PHP MYSQL Functions PHP Mysql Functions
File Handling File Handling
Error Handling Error Handling
DB Size DB Size
PHP Mail PHP Mail
String Tokens String Tokens
String Functions String Functions
String Functions List String Functions List1
String Functions List2 String Functions List2
Session Functions Session Functions
Cookies Functions Cookies Functions
Form Variables Form Variables
Running PHP from JS Running PHP from JS
Array To JS Array To JS
JS Array Array to PHP
Encryption Encryption
Common Header Common Header
Forums Ask Your Doubts
Scraps More about PHP
Feedback Feedback
 




mysql_fetch_lengths() Function in PHP


Tutorials »Php »

Topic

What is mysql_fetch_lengths() function in PHP?
How does mysql_fetch_lengths() works?




Explanation

This mysql_fetch_lengths() function returns the length of the contents of each field in a row.

Syntax

array mysql_fetch_lengths ( resource result)

This function returns a numeric array on success, or FALSE on failure or when there are no more rows.

This function mysql_fetch_lengths() returns an array and all elements of this array stores the length of data in each field of that row.

mysql_fetch_lengths() fetches the length of each field for the last row of data retrieved by mysql_fetch_array() , mysql_fetch_assoc() , mysql_fetch_object() , or mysql_fetch_row().

Example:


<?php

//Attempt to connect to the default database server
$link = mysql_connect("mysql_host", "mysql_user", "mysql_password")
or die ("Could not connect");

if (!mysql_select_db("my_database", $link)) {
echo " ERROR NO: " . mysql_errno($link) . "\n";
}

// Simple Select query
$query = "SELECT * FROM my_table";

// Execute the query
$mysql_result = mysql_query($query,$link)
or die ("Query '$query' failed with error message: \"" . mysql_error () . '"');

// Grab the data from the result handle
$row = mysql_fetch_row($mysql_result);

// Grab the length of the data in the field
$length = mysql_fetch_lengths ($mysql_result);

echo"<table > <tr <td>Field Name</td><td>Field Value</td><td>Data Length</td></tr>";

for ( $i = 0; $i < 3; $i++ ) {

// Grab the field name from from the result handle
$name = mysql_field_name( $mysql_result, $i );

//Display field name, field value and Data length
echo"<tr><td>$name</td><td>$row[$i]</td><td>$length[$i]</td></tr>";
}

echo"</table>";
mysql_close($link);

?>


In the above example mysql_fetch_row() function grabs the first record from the result of the executed query. Mysql_field_name() function fetches the field name of result row returned by mysql_query() function. Mysql_fetch_lengths() function returns an array containing length of each field in an array


RESULT:

Field NameField ValueData Length
ID11
NAMEPHP3
PARENTID21

See also: mysql_fetch_row()




Other Links

web hosting