Greetings everyone

i'm working on small php project and you help me a lot so far , what i want to know is how can i know the name of last column in a table i found away to know the last record in a column thats

SELECT LAST(column_name) FROM table_name

but i need to know the name of the last column in a table so i can store it in a value "what well happen is the user well not know the name of the column but when her press a button it will show the name of it" ... thank you

also if anyone know some good books please give me names

Recommended Answers

All 7 Replies

I cannot access my mysql part of my computer at the moment but this should work

$names = mysql_query('select * from TABLENAME', $connection);
$ArrayOfNames = mysql_field_name($names);

$ArrayOfNamesReversed = array_reverse($ArrayOfNames);
$last = $ArrayOfNamesReversed[0];
print($last);

i'm sorry bro but i think i don't get it i'm new to php & mysql

this is my code can you help me :)

<?php 


$db_host = "localhost";
$db_username = "root";
$db_pass = "";
$db_name = "el";


@mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect"); 
@mysql_select_db("$db_name") or die ("No database found");
echo"you are connected to the DB<br>";



$sql="SELECT * FROM attendace_info_old ORDER BY lastmodified DESC LIMIT 1;";
$res=mysql_query($sql);
$show =mysql_fetch_array($res)
echo "last lec ID is $show";
?>

In your case.

<?php 


$db_host = "localhost";
$db_username = "root";
$db_pass = "";
$db_name = "el";


@mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect"); 
@mysql_select_db("$db_name") or die ("No database found");
echo"you are connected to the DB<br>";



$q = mysql_query('SELECT * FROM attendace_info_old');
$ArrayOfNames = mysql_field_name($q);

$ArrayOfNamesReversed = array_reverse($ArrayOfNames);
$last = $ArrayOfNamesReversed[0];
print($last);

?>

Again, I can't test that out but it should work in theory.

Sir when i test it it give me this error it connected to DB but it didn't show the name of column

you are connected to the DB

Warning: mysql_field_name() expects exactly 2 parameters, 1 given in C:\xampp\htdocs\last.php on line 17

Warning: array_reverse() expects parameter 1 to be array, null given in C:\xampp\htdocs\last.php on line 19

Oh, right. This is a slopping method here.
replace line 17-21 with this

for($i = 0;$i < mysql_num_fields($q); ++$i) {
$Array[$i] = $meta->name;
}
$RevArray = array_reverse($Array);
$last = $RevArray[0];
print($last);

If that doesn't work I will have to load mysql on my virtualbox

<?php

$db_host = "localhost";
$db_username = "root";
$db_pass = "";
$db_name = "el";

@mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect"); 
@mysql_select_db("$db_name") or die ("No database found");
echo"you are connected to the DB<br>";

$result = mysql_query('SELECT * FROM attendace_info_old');

//Will return a positive integer
$fieldCount = mysql_num_fields( $result );

//Access field names by index starting from 0, so we must subtract 1 from the fieldCount
$lastFieldName = mysql_field_name( $result, ($fieldCount - 1) );

echo $lastFieldName;
Member Avatar for diafol
$sql = "SELECT * FROM table LIMIT 1";
$result = mysql_query($sql);
$last = mysql_num_fields($result) - 1; 
echo mysql_field_name($result,$last);

//EDIT

SOrry MS - was messing about while answering this, so took about 20 mins instaed of 2! Looks like I was just poncing off your answer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.