mysqli and mysqli_field_len [message #171578] |
Mon, 10 January 2011 23:31 |
DH
Messages: 1 Registered: January 2011
Karma: 0
|
Junior Member |
|
|
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)
|
|
|
Re: mysqli and mysqli_field_len [message #171579 is a reply to message #171578] |
Tue, 11 January 2011 00:02 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
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).
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
|
|
|
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: 0
|
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
|
|
|
Re: mysqli and mysqli_field_len [message #171581 is a reply to message #171578] |
Tue, 11 January 2011 09:56 |
Captain Paralytic
Messages: 204 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On Jan 10, 11:31 pm, DH <hockin...@gmail.com> 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)
Honestly, like this was difficult to find!
http://lmgtfy.com/?q=mysqli+field+length&l=1
|
|
|