Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Oct 2009
    Posts
    461
    Thanks
    7
    Thanked 3 Times in 3 Posts

    mysqli: how do i get a list of fields

    Is there a way to grab all field names and display them ?

    this is what i used to do...

    Code:
    mysql_field_name( $result ,$i);
    But can not find anything yet that is like this for mysqli

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    You use [mysqli_]fetch_field_direct method. It returns an object and two of the properties are name and orgname.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    Regular Coder
    Join Date
    Oct 2009
    Posts
    461
    Thanks
    7
    Thanked 3 Times in 3 Posts
    I did try that one, but not understanding how to get the field names.

    Code:
    $result = db_query($mysqli, $query);	$columns = $result->field_count;
    
    for($i = 1; $i < $columns; $i++) {
    							echo($result->fetch_field_direct());
    							// echo($result->fetch_field_direct()->name);
    							?><?
    							} ?><br><?
    									while($row = $result->fetch_assoc()) {
    											foreach ($row as $r) { ?><? echo $r; ?><?
    											} ?><br><?
    									}

  • #4
    Regular Coder
    Join Date
    Oct 2009
    Posts
    461
    Thanks
    7
    Thanked 3 Times in 3 Posts
    ok i think i have it?

    Code:
    echo($result->fetch_field_direct($i)->name);

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Yep that should work in the newer versions of PHP.
    It won't work in older (and I'm talking like 5.3 here as older, but I'm not 100% sure when it was implemented). The reason is that PHP wasn't designed until very recently to allow dereferencing of the return of a function / method call, only on assigned zval's themselves. So this wouldn't work in older versions. The work around is very easy, which I'd suggest doing at least for the time being anyway:
    PHP Code:
    $oField $result->fetch_field_direct($i);
    print 
    $oField->name
    'name' is the aliased name if it has been aliased, while orgname would be the actual name of the property in the dbms.

    MySQLi also has the fetch_fields method on the mysqli_result object which returns an array of objects representing each fields. Its the same as the fetch_field_direct except the direct asks which field you want.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #6
    Regular Coder
    Join Date
    Oct 2009
    Posts
    461
    Thanks
    7
    Thanked 3 Times in 3 Posts
    ok i shall try that for now, thanks


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •