Documentation Home
MySQL 5.6 リファレンスマニュアル
Download this Manual
PDF (US Ltr) - 27.1Mb
PDF (A4) - 27.1Mb
HTML Download (TGZ) - 7.2Mb
HTML Download (Zip) - 7.2Mb


23.8.7.20 mysql_fetch_lengths()

unsigned long *mysql_fetch_lengths(MYSQL_RES *result)

説明

結果セット内の現在の行のカラムの長さを返します。フィールド値をコピーする予定がある場合、この長さ情報は、strlen() の呼び出しを避けることができるため、最適化にも役立ちます。さらに、結果セットにバイナリデータが含まれている場合、strlen() は NULL 文字を含むフィールドについて誤った結果を返すため、この関数を使用して、データのサイズを判断する必要があります

空のカラムおよび NULL 値を含むカラムの長さはゼロです。これらの 2 つのケースを区別する方法を確認するには、mysql_fetch_row() の説明を参照してください。

戻り値

各カラム (終端の NULL 文字を含まない) のサイズを表す符号なし long 整数の配列。エラーが発生した場合は NULL

エラー

mysql_fetch_lengths() は結果セットの現在の行に対してのみ有効です。それを mysql_fetch_row() を呼び出す前または結果内のすべての行を取得したあとに呼び出すと、それは NULL を返します。

MYSQL_ROW row;
unsigned long *lengths;
unsigned int num_fields;
unsigned int i;

row = mysql_fetch_row(result);
if (row)
{
    num_fields = mysql_num_fields(result);
    lengths = mysql_fetch_lengths(result);
    for(i = 0; i < num_fields; i++)
    {
         printf("Column %u is %lu bytes in length.\n",
                i, lengths[i]);
    }
}

User Comments
  Posted by Jennifer Fairchild on August 8, 2003
While the above example doesn't show it, it's probably a good idea to malloc() the lengths variable using lengths = malloc( sizeof( unsigned long ) * num_fields ) prior to packing your column lengths in, and free()'ing it after you're done with them.

I say 'probably' a good idea, because I'm honestly not sure. The MYSQL_RES type with mysql_store_result automatically allocated the memory.
  Posted by on September 23, 2003
No Jennifer, that's not necessary.

It appears that the mysql_fetch_lengths() returns a pointer to data inside 'result' - the memory is allocated and freed with 'result'.

My code is similar to the example and I haven't run into any problems.
  Posted by Chris Newey on February 6, 2005
malloc is definately not a good idea.

malloc acquires some memory and returns a pointer to that memory. The call to fetch_lengths is going to overwrite that pointer.

A subsequent call to free using the value now in the lengths stack variable is going to attempt to free some memory inside the result structure. Exactly what would happen then I can't be bothered to investigate. I think, stress think, that free will give up 'cos it won't recognize the address passed to it.

The bad news is the pointer to malloced memory has now gone walkies and a memory leak has been created - whoops <g>
Sign Up Login You must be logged in to post a comment.