View Full Version : mysqli: how do i get a list of fields

02-01-2013, 11:08 AM
Is there a way to grab all field names and display them ?

this is what i used to do...

mysql_field_name( $result ,$i);

But can not find anything yet that is like this for mysqli

02-01-2013, 12:25 PM
You use [mysqli_]fetch_field_direct method. It returns an object and two of the properties are name and orgname.

02-01-2013, 12:39 PM
I did try that one, but not understanding how to get the field names.

$result = db_query($mysqli, $query); $columns = $result->field_count;

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

02-01-2013, 12:45 PM
ok i think i have it?


02-01-2013, 08: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:

$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.

02-01-2013, 10:28 PM
ok i shall try that for now, thanks