Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-01-2013, 10:08 AM   PM User | #1
needsomehelp
Regular Coder

 
Join Date: Oct 2009
Posts: 306
Thanks: 4
Thanked 3 Times in 3 Posts
needsomehelp can only hope to improve
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
needsomehelp is offline   Reply With Quote
Old 02-01-2013, 11:25 AM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
You use [mysqli_]fetch_field_direct method. It returns an object and two of the properties are name and orgname.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 02-01-2013, 11:39 AM   PM User | #3
needsomehelp
Regular Coder

 
Join Date: Oct 2009
Posts: 306
Thanks: 4
Thanked 3 Times in 3 Posts
needsomehelp can only hope to improve
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 is offline   Reply With Quote
Old 02-01-2013, 11:45 AM   PM User | #4
needsomehelp
Regular Coder

 
Join Date: Oct 2009
Posts: 306
Thanks: 4
Thanked 3 Times in 3 Posts
needsomehelp can only hope to improve
ok i think i have it?

Code:
echo($result->fetch_field_direct($i)->name);
needsomehelp is offline   Reply With Quote
Old 02-01-2013, 07:29 PM   PM User | #5
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 02-01-2013, 09:28 PM   PM User | #6
needsomehelp
Regular Coder

 
Join Date: Oct 2009
Posts: 306
Thanks: 4
Thanked 3 Times in 3 Posts
needsomehelp can only hope to improve
ok i shall try that for now, thanks
needsomehelp is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:39 AM.


Advertisement
Log in to turn off these ads.