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