Re: mysqli and mysqli_field_len [message #171580 is a reply to message #171579] |
Tue, 11 January 2011 00:40 |
Denis McMahon
Messages: 634 Registered: September 2010
Karma:
|
Senior Member |
|
|
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
|
|
|