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? Quote Link to comment 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. Quote Link to comment 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; Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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); } ?> Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
renj0806 Posted February 19, 2007 Author Share Posted February 19, 2007 That is what i wanted. Thanks! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.