CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   MySQL (http://www.codingforums.com/forumdisplay.php?f=7)
-   -   mysqli: how do i get a list of fields (http://www.codingforums.com/showthread.php?t=286863)

needsomehelp 02-01-2013 10:08 AM

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

Fou-Lu 02-01-2013 11:25 AM

You use [mysqli_]fetch_field_direct method. It returns an object and two of the properties are name and orgname.

needsomehelp 02-01-2013 11:39 AM

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><?
                                                                        }


needsomehelp 02-01-2013 11:45 AM

ok i think i have it?

Code:

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

Fou-Lu 02-01-2013 07:29 PM

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.

needsomehelp 02-01-2013 09:28 PM

ok i shall try that for now, thanks


All times are GMT +1. The time now is 10:26 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.