Part of the EllisLab Network
   
 
$this->db->field_data() max_length
Posted: 27 September 2008 05:04 PM   [ Ignore ]  
Grad Student
Avatar
Rank
Total Posts:  64
Joined  04-17-2008

Hi,

I’m working on a application that uses $this->db->field_data() to get the field meta-data of a MySQL table (MyISAM storage engine). However when I check the results, max_length is always 0 even though my fields have lengths (at least for blob, string, and int types (as returned by the function), I haven’t tested any others).

I could be completely wrong in my understanding of how this works (script or DB wise), but if I have a length set for my fields, shouldn’t it show up in the returned data? I’ve never used this function (or even attempted the same thing outside of CI).

I’ve attached a screen-shot of my table’s setup.

Image Attachments
Picture 2.png
Click thumbnail to see full-size image
 Signature 

Personal Site: http://joshuakendall.com/
Twitter: @jtkendall | SporkCMS: http://github.com/jtkendall/sporkcms/

Profile
 
 
Posted: 27 September 2008 07:33 PM   [ Ignore ]   [ # 1 ]  
Grad Student
Avatar
Rank
Total Posts:  64
Joined  04-17-2008

I just noticed it’s also not returning the defaults.

 Signature 

Personal Site: http://joshuakendall.com/
Twitter: @jtkendall | SporkCMS: http://github.com/jtkendall/sporkcms/

Profile
 
 
Posted: 27 September 2008 11:10 PM   [ Ignore ]   [ # 2 ]  
Lab Technician
RankRankRankRank
Total Posts:  1040
Joined  06-19-2007

Let’s see the code.

 Signature 

My new therapist is working with me every day, the third one gave up… ohh

Profile
 
 
Posted: 27 September 2008 11:31 PM   [ Ignore ]   [ # 3 ]  
Grad Student
Avatar
Rank
Total Posts:  64
Joined  04-17-2008

I’m just doing the same thing as what’s in the user guide. Though in the user guide it doesn’t state that the field’s default will be in the meta data, even though when I print the object it’s there.

This is just a quick example of the code. All I do after this step is run a foreach on the fields variable and send the field metadata to another method. But that doesn’t effect the object.

public function test()
{
    $fields
= $this->db->field_data('test');
    echo
'<pre>';
    
print_r($fields);    
    echo
'</pre>';
}

I was testing to see what each field type (name key in this case) returned as it’s actual type and this is what’s returned:

Array
(
    
[0] => stdClass Object
        
(
            
[name] => id
            [type]
=> int
            [default]
=>
            
[max_length] => 0
            [primary_key]
=> 1
        
)

    
[1] => stdClass Object
        
(
            
[name] => tinyint
            [type]
=> int
            [default]
=>
            
[max_length] => 0
            [primary_key]
=> 0
        
)

    
[2] => stdClass Object
        
(
            
[name] => smallint
            [type]
=> int
            [default]
=>
            
[max_length] => 0
            [primary_key]
=> 0
        
)

    
[3] => stdClass Object
        
(
            
[name] => mediumint
            [type]
=> int
            [default]
=>
            
[max_length] => 0
            [primary_key]
=> 0
        
)

    
[4] => stdClass Object
        
(
            
[name] => int
            [type]
=> int
            [default]
=>
            
[max_length] => 0
            [primary_key]
=> 0
        
)

    
[5] => stdClass Object
        
(
            
[name] => bigint
            [type]
=> int
            [default]
=>
            
[max_length] => 0
            [primary_key]
=> 0
        
)

    
[6] => stdClass Object
        
(
            
[name] => float
            [type]
=> real
            [default]
=>
            
[max_length] => 0
            [primary_key]
=> 0
        
)

    
[7] => stdClass Object
        
(
            
[name] => decimal
            [type]
=> real
            [default]
=>
            
[max_length] => 0
            [primary_key]
=> 0
        
)

    
[8] => stdClass Object
        
(
            
[name] => double
            [type]
=> real
            [default]
=>
            
[max_length] => 0
            [primary_key]
=> 0
        
)

    
[9] => stdClass Object
        
(
            
[name] => date
            [type]
=> date
            [default]
=>
            
[max_length] => 0
            [primary_key]
=> 0
        
)

    
[10] => stdClass Object
        
(
            
[name] => datetime
            [type]
=> datetime
            [default]
=>
            
[max_length] => 0
            [primary_key]
=> 0
        
)

    
[11] => stdClass Object
        
(
            
[name] => timestamp
            [type]
=> timestamp
            [default]
=>
            
[max_length] => 0
            [primary_key]
=> 0
        
)

    
[12] => stdClass Object
        
(
            
[name] => time
            [type]
=> time
            [default]
=>
            
[max_length] => 0
            [primary_key]
=> 0
        
)

    
[13] => stdClass Object
        
(
            
[name] => year
            [type]
=> year
            [default]
=>
            
[max_length] => 0
            [primary_key]
=> 0
        
)

    
[14] => stdClass Object
        
(
            
[name] => char
            [type]
=> string
            [default]
=>
            
[max_length] => 0
            [primary_key]
=> 0
        
)

    
[15] => stdClass Object
        
(
            
[name] => varchar
            [type]
=> string
            [default]
=>
            
[max_length] => 0
            [primary_key]
=> 0
        
)

    
[16] => stdClass Object
        
(
            
[name] => tinyblob
            [type]
=> blob
            [default]
=>
            
[max_length] => 0
            [primary_key]
=> 0
        
)

    
[17] => stdClass Object
        
(
            
[name] => blob
            [type]
=> blob
            [default]
=>
            
[max_length] => 0
            [primary_key]
=> 0
        
)

    
[18] => stdClass Object
        
(
            
[name] => text
            [type]
=> blob
            [default]
=>
            
[max_length] => 0
            [primary_key]
=> 0
        
)

    
[19] => stdClass Object
        
(
            
[name] => mediumblob
            [type]
=> blob
            [default]
=>
            
[max_length] => 0
            [primary_key]
=> 0
        
)

    
[20] => stdClass Object
        
(
            
[name] => mediumtext
            [type]
=> blob
            [default]
=>
            
[max_length] => 0
            [primary_key]
=> 0
        
)

    
[21] => stdClass Object
        
(
            
[name] => longblob
            [type]
=> blob
            [default]
=>
            
[max_length] => 0
            [primary_key]
=> 0
        
)

    
[22] => stdClass Object
        
(
            
[name] => longtext
            [type]
=> blob
            [default]
=>
            
[max_length] => 0
            [primary_key]
=> 0
        
)

    
[23] => stdClass Object
        
(
            
[name] => enum
            [type]
=> string
            [default]
=>
            
[max_length] => 0
            [primary_key]
=> 0
        
)

    
[24] => stdClass Object
        
(
            
[name] => set
            [type]
=> string
            [default]
=>
            
[max_length] => 0
            [primary_key]
=> 0
        
)

)

As you can see default is empty and max_length is 0. I’ve set defaults on a few of the fields and all of the string and int fields have a length.

 Signature 

Personal Site: http://joshuakendall.com/
Twitter: @jtkendall | SporkCMS: http://github.com/jtkendall/sporkcms/

Profile
 
 
Posted: 27 September 2008 11:47 PM   [ Ignore ]   [ # 4 ]  
Lab Technician
RankRankRankRank
Total Posts:  1040
Joined  06-19-2007

Don’t know…I’m perplexed.  What version of MySQL?

 Signature 

My new therapist is working with me every day, the third one gave up… ohh

Profile
 
 
Posted: 27 September 2008 11:50 PM   [ Ignore ]   [ # 5 ]  
Lab Technician
RankRankRankRank
Total Posts:  1040
Joined  06-19-2007

Ah…something else just crossed my mind too.  This might be a privilege problem.  Is this a production Db server or development?  Are you controlling privs?  Does the DB user have ALL or restricted. I’m thinking, off the top of my head, that SHOW is likely required.  I’ll check.

Randy

 Signature 

My new therapist is working with me every day, the third one gave up… ohh

Profile
 
 
Posted: 28 September 2008 12:08 AM   [ Ignore ]   [ # 6 ]  
Lab Technician
RankRankRankRank
Total Posts:  1040
Joined  06-19-2007

Nope…not a privileges issues.  All you need is select.  Still unsure about the version.  I will confirm that you what you have is correct and that CI is working correctly.

MySQL, it seems, is not returning the expected results for some reason.  I’m not sure all the information needed has been exposed quite yet.  As in, this may be a MySQL configuration issue.

Randy

 Signature 

My new therapist is working with me every day, the third one gave up… ohh

Profile
 
 
Posted: 28 September 2008 12:19 AM   [ Ignore ]   [ # 7 ]  
Grad Student
Avatar
Rank
Total Posts:  64
Joined  04-17-2008

It’s a local development server running on my MacBook. MySQL version is 5.0.51b. I’m logged in as root, and I have every available privilege set. I just did a quick test on one of my production servers (running MySQL 5.0) and same results. No defaults and 0 for a max_length.

 Signature 

Personal Site: http://joshuakendall.com/
Twitter: @jtkendall | SporkCMS: http://github.com/jtkendall/sporkcms/

Profile
 
 
Posted: 28 September 2008 12:37 AM   [ Ignore ]   [ # 8 ]  
Lab Technician
RankRankRankRank
Total Posts:  1040
Joined  06-19-2007

Running in strict mode?

 Signature 

My new therapist is working with me every day, the third one gave up… ohh

Profile
 
 
Posted: 28 September 2008 08:40 AM   [ Ignore ]   [ # 9 ]  
Grad Student
Avatar
Rank
Total Posts:  64
Joined  04-17-2008

Nope, not running in strict mode.

 Signature 

Personal Site: http://joshuakendall.com/
Twitter: @jtkendall | SporkCMS: http://github.com/jtkendall/sporkcms/

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 819, on March 11, 2010 11:15 AM
Total Registered Members: 119870 Total Logged-in Users: 59
Total Topics: 126008 Total Anonymous Users: 3
Total Replies: 662859 Total Guests: 496
Total Posts: 788867    
Members ( View Memberlist )