russthebarber Posted April 5, 2012 Share Posted April 5, 2012 Can someone please tell me how to use the "SHOW FIELDS FROM $mytable" in php? I understand how the MySql works but need to get these fields back into an array in php. That is the part I am strruggling with.. I normally return rows of data from my table and use a while loop. Quote Link to comment https://forums.phpfreaks.com/topic/260383-show-fields-from-table-and-php/ Share on other sites More sharing options...
trq Posted April 5, 2012 Share Posted April 5, 2012 I normally return rows of data from my table and use a while loop. And this query would be no different. Quote Link to comment https://forums.phpfreaks.com/topic/260383-show-fields-from-table-and-php/#findComment-1334582 Share on other sites More sharing options...
russthebarber Posted April 5, 2012 Author Share Posted April 5, 2012 I don't know what you mean by that. A more in depth answer would be helpful while ($row = mysql_fetch_array($result)) { $product_name = $row['product_name']; } They are not rows so what goes in place of $row and in place of $row['product_name'] for example ??? Quote Link to comment https://forums.phpfreaks.com/topic/260383-show-fields-from-table-and-php/#findComment-1334605 Share on other sites More sharing options...
Jessica Posted April 5, 2012 Share Posted April 5, 2012 It will return as rows. Did you *try* it at all? Quote Link to comment https://forums.phpfreaks.com/topic/260383-show-fields-from-table-and-php/#findComment-1334614 Share on other sites More sharing options...
russthebarber Posted April 5, 2012 Author Share Posted April 5, 2012 Here is the last thing I tried. i'm getting closer but need to know what to put in place of xxxx in the while loop to get the field name. $sql = "SHOW FIELDS FROM apartmentoUsers"; $result = mysql_query($sql); while ($row = mysql_fetch_array($result)) { echo xxxxx;//what goes here? } Quote Link to comment https://forums.phpfreaks.com/topic/260383-show-fields-from-table-and-php/#findComment-1334621 Share on other sites More sharing options...
russthebarber Posted April 5, 2012 Author Share Posted April 5, 2012 In case this helps anyone even sillier than me. I've just tried this: $sql = "SHOW FIELDS FROM apartmentoUsers"; $result = mysql_query($sql); while ($row = mysql_fetch_array($result)) { echo $row[0]; } ....which works. Quote Link to comment https://forums.phpfreaks.com/topic/260383-show-fields-from-table-and-php/#findComment-1334627 Share on other sites More sharing options...
trq Posted April 5, 2012 Share Posted April 5, 2012 Have you executed this query from within MySql to see what it might look like? Your query will return something like: [pre] +------------------+---------------------+------+-----+---------------------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------------+---------------------+------+-----+---------------------+----------------+ | id | bigint(20) unsigned | NO | PRI | NULL | auto_increment | | somefield | varchar(255) | NO | | | | +------------------+---------------------+------+-----+---------------------+----------------+ [/pre] Armed with this information you can see to use: $sql = "SHOW FIELDS FROM apartmentoUsers"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_assoc($result)) { echo $row['Filed'] . '<br />'; echo $row['Type'] . '<br />'; echo $row['Null'] . '<br />'; echo $row['Key'] . '<br />'; echo $row['Default'] . '<br />'; echo $row['Extra'] . '<br />'; } } } Of course, $row is just an array, so you could also have looped through it also. $sql = "SHOW FIELDS FROM apartmentoUsers"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_assoc($result)) { foreach ($row as $val) { echo $val . '<br />'; } } } } Quote Link to comment https://forums.phpfreaks.com/topic/260383-show-fields-from-table-and-php/#findComment-1334629 Share on other sites More sharing options...
russthebarber Posted April 5, 2012 Author Share Posted April 5, 2012 Thanks foe the extra info. I can now see how it works. Quote Link to comment https://forums.phpfreaks.com/topic/260383-show-fields-from-table-and-php/#findComment-1334661 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.