CodeCharge Studio
search Register Login  

Visual PHP Web Development

Visually Create Internationalized Web Applications, Web Reports, Calendars, and more.
CodeCharge.com

YesSoftware Forums -> CodeCharge Studio -> PHP

 getting field name from $db object

Print topic Send  topic

Author Message
Ganesan

Posts: 25
Posted: 06/08/2012, 3:00 AM

Hi,
I want to get the field name form the $db object.
For example:
Table name: student
fieldnames:
sm_studno,sm_fullname,sm_dob

values:
'STUD01','ALEX',2004-06-10
'STUD02','SUJAN',1995-09-28

Code:

$db = new clsDBcon();
$db->query("select * from student limit 1");
$db->next_record();

if I use $db->f(0), I can get the value of the 1st field 'STUD01'.
How do I get the field name lke "sm_studno" for the $db?

Any help?
Thanks much.
View profile  Send private message
DataDoIT
Posted: 06/08/2012, 8:28 AM

$db->f("sm_studno")
Ganesan

Posts: 25
Posted: 06/08/2012, 6:53 PM

Hi DataDoIT,
Thanks much for your reply.

your solution give the value of the field same as $db->f(0).

Anyway I found the solution.
the command is pg_field_name. for e.g:

$ar="";
for ($i=0; $i<$db->num_fields(); $i++)
{
$fld=@pg_field_name($db->Query_ID, $i);
$data[$fld]=$db->f($i);
}

Best regards,
View profile  Send private message
DataDoIT
Posted: 06/09/2012, 7:56 AM

Ah. Field 'name', not 'value'. I misread.

Here's a CCS-ified function to put the field names into an array for
later use.

function GetFieldNames()
{

global $Connection1;

$db = new clsDBConnection1();
$db->query("SHOW FIELDS FROM MyDatabase.MyTable");
$TheFields = array();
while ($db->next_record()) {
$TheFields[] = $db->f("Field");
}

$db->close();
return $TheFields;

} //End GetFieldNames
Ganesan

Posts: 25
Posted: 06/12/2012, 3:35 PM

Thank you. I will try.
View profile  Send private message
laneoc

Posts: 154
Posted: 06/12/2012, 5:25 PM

I had a scenario where I needed to pull the field names from a query constructed by the user. Since I didn't know the table(s) nor the number of fields, I used the following approach.

$field_num = $DB->num_fields();  
for($i=0;$i<$field_num;$i++)  
	{  
	mysqli_field_seek($DB->Query_ID, $i);  
	$finfo = mysqli_fetch_field($DB->Query_ID);  
	$field_name = $finfo->name;  
	//     Do your thing with each field name here.  
	}  


Lane
_________________
Lane
View profile  Send private message

Add new topic Subscribe to topic   


These are Community Forums for users to exchange information.
If you would like to obtain technical product help please visit http://support.yessoftware.com.

Internet Database

Visually create Web enabled database applications in minutes.
CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


Powered by UltraApps Forum created with CodeCharge Studio
Copyright © 2003-2004 by UltraApps.com  and YesSoftware, Inc.