Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/274171-returning-sql-column-names/
Share on other sites

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

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 by Mko

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?

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.