burgergetsbored Posted February 7, 2013 Share Posted February 7, 2013 Hey all I'm trying to do is echo out the column names from my sql table rather than the content. Everyone online seems to recommend mysql_fetch_field() but phps documentation say it's becoming deprecated now ? And other examples use quite a bit of code. Is there no simple way of doing this? Quote Link to comment https://forums.phpfreaks.com/topic/274171-returning-sql-column-names/ Share on other sites More sharing options...
Mko Posted February 7, 2013 Share Posted February 7, 2013 (edited) You could always just use mysqli_fetch_field(). Read more: http://php.net/manua...etch-fields.php Edited February 7, 2013 by Mko Quote Link to comment https://forums.phpfreaks.com/topic/274171-returning-sql-column-names/#findComment-1410826 Share on other sites More sharing options...
burgergetsbored Posted February 7, 2013 Author Share Posted February 7, 2013 I've never used mysqli before, and all my current code is in sql or does that not matter ? I tried using $query=mysql_query('SHOW COLUMNS FROM table ') or die(mysql_error()); while($field=mysql_fetch_object($query)){ $fields[]=$field; }; print_r($fields); foreach($fields as $key=>$field){ echo $field->Field.'</br>'; which worked for most of the columns but the first one produced a massive error Quote Link to comment https://forums.phpfreaks.com/topic/274171-returning-sql-column-names/#findComment-1410833 Share on other sites More sharing options...
Mko Posted February 7, 2013 Share Posted February 7, 2013 (edited) I've never used mysqli before, and all my current code is in sql or does that not matter ? I tried using $query=mysql_query('SHOW COLUMNS FROM table ') or die(mysql_error()); while($field=mysql_fetch_object($query)){ $fields[]=$field; }; print_r($fields); foreach($fields as $key=>$field){ echo $field->Field.'</br>'; which worked for most of the columns but the first one produced a massive error You'd need to change up some features of your code. For connecting, use $mysqli = new mysqli("localhost", "my_user", "my_password", "world"); And for querying, use $mysqli->query($query); Most of the MySQL features are the same in MySQLi, but they just include the i in the function name. Be sure to go to php.net to read up on the functionality of all the MySQLi functions, as not all of them are completely alike. Edited February 7, 2013 by Mko Quote Link to comment https://forums.phpfreaks.com/topic/274171-returning-sql-column-names/#findComment-1410834 Share on other sites More sharing options...
burgergetsbored Posted February 7, 2013 Author Share Posted February 7, 2013 Ah okay then. So would there be no way of doing it in just SQL then? Quote Link to comment https://forums.phpfreaks.com/topic/274171-returning-sql-column-names/#findComment-1410838 Share on other sites More sharing options...
Mko Posted February 7, 2013 Share Posted February 7, 2013 Ah okay then. So would there be no way of doing it in just SQL then? You can always opt for using MySQL, but it has become depreciated. Quote Link to comment https://forums.phpfreaks.com/topic/274171-returning-sql-column-names/#findComment-1410842 Share on other sites More sharing options...
burgergetsbored Posted February 7, 2013 Author Share Posted February 7, 2013 I'd rather use SQL as this is the only bit I'm struggling with and all the rest works okay for this small project! But if there's no real easy way then that may become a problem. Quote Link to comment https://forums.phpfreaks.com/topic/274171-returning-sql-column-names/#findComment-1410847 Share on other sites More sharing options...
Mko Posted February 7, 2013 Share Posted February 7, 2013 I'd rather use SQL as this is the only bit I'm struggling with and all the rest works okay for this small project! But if there's no real easy way then that may become a problem. Is your issue that mysql_fetch_field() is depreciated so you're getting a warning or do you have another error? Quote Link to comment https://forums.phpfreaks.com/topic/274171-returning-sql-column-names/#findComment-1410848 Share on other sites More sharing options...
Barand Posted February 7, 2013 Share Posted February 7, 2013 An easy way is to use xxxxx_fetch_assoc the get the array keys. There's an example here http://forums.phpfreaks.com/topic/274059-select-from-tablea-where-1/?do=findComment&comment=1410285 Quote Link to comment https://forums.phpfreaks.com/topic/274171-returning-sql-column-names/#findComment-1410858 Share on other sites More sharing options...
Jessica Posted February 8, 2013 Share Posted February 8, 2013 I'd rather use SQL as this is the only bit I'm struggling with and all the rest works okay for this small project! But if there's no real easy way then that may become a problem. SQL = server query language MySQL = a specific database that uses a specific dialect of SQL. Mysqli = the improved mysql interface for PHP. You are using SQL no matter what. If you want to use mysqli functions you have to change all of your mysql_ functions to mysql_, and the manual documents the functions. But if you were doing that you might as well switch to PDO. You don't need to switch in order to get your column names. You should switch if this is new code you're writing now. Quote Link to comment https://forums.phpfreaks.com/topic/274171-returning-sql-column-names/#findComment-1410889 Share on other sites More sharing options...
Barand Posted February 8, 2013 Share Posted February 8, 2013 SQL = server query language Erm... Structured Query Language Quote Link to comment https://forums.phpfreaks.com/topic/274171-returning-sql-column-names/#findComment-1410977 Share on other sites More sharing options...
Jessica Posted February 8, 2013 Share Posted February 8, 2013 Yeah I have no idea why I said server. Lol!! Thanks Barand Quote Link to comment https://forums.phpfreaks.com/topic/274171-returning-sql-column-names/#findComment-1411002 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.