Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

mysqli and mysqli_field_len

365 views
Skip to first unread message

DH

unread,
Jan 10, 2011, 6:31:28 PM1/10/11
to
I was migrating a large project from PHP's mysql functions to mysqli
functions. One project relies on mysql_field_len() and I do NOT find a
comparable mysqli_field_len().

How the heck can one use mysqli to grab the defined field length for
all columns?

I can't believe mysqli_field_len() does not exist, and I don't find a
new function to grab the defined length (sure, the data length can be
fetched, but I don't care about that, for example I need the length
255 from varchar(255) or 30 from varchar(30)

Jerry Stuckle

unread,
Jan 10, 2011, 7:02:04 PM1/10/11
to

It's part of the result set. See MySQLi_result->lengths() or
mysqli_fetch_lengths($result).

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstu...@attglobal.net
==================

Denis McMahon

unread,
Jan 10, 2011, 7:40:26 PM1/10/11
to
On 11/01/11 00:02, Jerry Stuckle wrote:
> On 1/10/2011 6:31 PM, DH wrote:
>> I was migrating a large project from PHP's mysql functions to mysqli
>> functions. One project relies on mysql_field_len() and I do NOT find a
>> comparable mysqli_field_len().
>>
>> How the heck can one use mysqli to grab the defined field length for
>> all columns?
>>
>> I can't believe mysqli_field_len() does not exist, and I don't find a
>> new function to grab the defined length (sure, the data length can be
>> fetched, but I don't care about that, for example I need the length
>> 255 from varchar(255) or 30 from varchar(30)
>
> It's part of the result set. See MySQLi_result->lengths() or
> mysqli_fetch_lengths($result).

Try the following:

<?php
$fwidths = array();
$link = mysqli_connect('host','user','password','db');
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n",mysqli_connect_error());
exit();
}
$result = mysqli_query($link,"SELECT * FROM table");
if ($result) {
$fnr = mysqli_num_fields($result);
while ($fnr --) {
$metadata = mysqli_fetch_field_direct($result, $fnr);
$fwidths[$metadata['name']] = $metadata['length'];
}
mysqli_free_result($result);
}
else {
printf("Query failed: %s\n",mysqli_error($link));
}
if (count($fwidths)) print_r($fwidths);
else echo "$fwidths is empty\n";
?>

I think $fwidths['column-name'] should then contain column-width as
defined in the database schema, although I didn't actually test this.

Rgds

Denis McMahon

Captain Paralytic

unread,
Jan 11, 2011, 4:56:40 AM1/11/11
to

Honestly, like this was difficult to find!

http://lmgtfy.com/?q=mysqli+field+length&l=1

0 new messages