renj0806 Posted February 19, 2007 Share Posted February 19, 2007 For example ive got a query: SELECT * FROM userstable; And my userstable have the ff fields: -id -username -password How can i retrieve the FIELD NAMES (id,username and password), not the data and put it in a php variable? Link to comment https://forums.phpfreaks.com/topic/39132-solved-retrieving-sql-field-names/ Share on other sites More sharing options...
skali Posted February 19, 2007 Share Posted February 19, 2007 You can use: $query = 'SHOW COLUMNS FROM [table name]'; $rs = mysql_query($query); which will list all the fields alongwith various details. Link to comment https://forums.phpfreaks.com/topic/39132-solved-retrieving-sql-field-names/#findComment-188456 Share on other sites More sharing options...
renj0806 Posted February 19, 2007 Author Share Posted February 19, 2007 What if i only want to show certain columns like when i do this query: SELECT username, password FROM userstable; Link to comment https://forums.phpfreaks.com/topic/39132-solved-retrieving-sql-field-names/#findComment-188459 Share on other sites More sharing options...
monk.e.boy Posted February 19, 2007 Share Posted February 19, 2007 I don't understand your question $query = 'SHOW COLUMNS FROM [table name]'; $rs = mysql_query($query); Did you run the above code and look at the results? monk.e.boy Link to comment https://forums.phpfreaks.com/topic/39132-solved-retrieving-sql-field-names/#findComment-188476 Share on other sites More sharing options...
renj0806 Posted February 19, 2007 Author Share Posted February 19, 2007 Oh... im sorry. What i meant was if I only want the username and password to be retrieved. Not including the id. Anyways, ive tried the query above and yes, i got all the fields and the details for each field. Now, how am i going to assign a variable to the field name only. Link to comment https://forums.phpfreaks.com/topic/39132-solved-retrieving-sql-field-names/#findComment-188479 Share on other sites More sharing options...
ToonMariner Posted February 19, 2007 Share Posted February 19, 2007 'SHOW COLUMNS FROM [table name]' will return a result set of info about the table to see the resulst do this... <?php $query = 'SHOW COLUMNS FROM [table name]'; $rs = mysql_query($query); while ($row = mysql_fetch_assoc($rs)) { print_r($row); } ?> Link to comment https://forums.phpfreaks.com/topic/39132-solved-retrieving-sql-field-names/#findComment-188482 Share on other sites More sharing options...
renj0806 Posted February 19, 2007 Author Share Posted February 19, 2007 'SHOW COLUMNS FROM [table name]' will return a result set of info about the table to see the resulst do this... <?php $query = 'SHOW COLUMNS FROM [table name]'; $rs = mysql_query($query); while ($row = mysql_fetch_assoc($rs)) { print_r($row); } ?> Yep, I saw the results. What i want now is how to assign a variable for the field name only, not including all those other stuff like the field type. Link to comment https://forums.phpfreaks.com/topic/39132-solved-retrieving-sql-field-names/#findComment-188485 Share on other sites More sharing options...
ToonMariner Posted February 19, 2007 Share Posted February 19, 2007 just pick out the 'Field' element from the array... <?php $query = 'SHOW COLUMNS FROM [table name]'; $rs = mysql_query($query); $field = array(); while ($row = mysql_fetch_assoc($rs)) { $field[] = $row['Field']; } ?> that will give you an array of all the fields. Link to comment https://forums.phpfreaks.com/topic/39132-solved-retrieving-sql-field-names/#findComment-188508 Share on other sites More sharing options...
renj0806 Posted February 19, 2007 Author Share Posted February 19, 2007 That is what i wanted. Thanks! Link to comment https://forums.phpfreaks.com/topic/39132-solved-retrieving-sql-field-names/#findComment-188510 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.