Jump to content

Displaying Column Names


Lyleyboy

Recommended Posts

Morning all,

 

I have a question regarding the ability to show the column names from my table/query.

What I'm looking to do is to be able to have my users enter their own query via a builder (Which I haven't built yet).

I've found a little test script from the net but it shows them in an array.

 

My code is

<?php
  
include('includes/inc.conn.php');
  
mysql_select_db("discover_messaging");


$result = mysql_query("SHOW COLUMNS FROM `messages`");

if (mysql_num_rows($result) > 0) {
    while ($row = mysql_fetch_assoc($result)) {
        print_r($row);
    }
}
    
?>

 

Which outputs

Array ( [Field] => msg_id [Type] => int(11) [Null] => NO [Key] => PRI [Default] => [Extra] => auto_increment ) Array ( [Field] => msg_to [Type] => varchar(99) [Null] => NO [Key] => [Default] => [Extra] => ) Array ( [Field] => msg_from [Type] => varchar(99) [Null] => NO [Key] => [Default] => [Extra] => ) Array ( [Field] => msg_subject [Type] => varchar(255) [Null] => NO [Key] => [Default] => [Extra] => ) Array ( [Field] => msg_body [Type] => text [Null] => NO [Key] => [Default] => [Extra] => ) Array ( [Field] => read_flg [Type] => varchar(3) [Null] => NO [Key] => [Default] => off [Extra] => ) Array ( [Field] => sent_date [Type] => varchar(255) [Null] => NO [Key] => [Default] => [Extra] => )

 

Is there a way to have the field names come out of the array so that I can do something like?

<table>
<tr>
<td>msg_id</td>
<td>msg_to</td>
etc ...

 

Thanks all.

 

Link to comment
https://forums.phpfreaks.com/topic/177882-displaying-column-names/
Share on other sites

Hi all,

 

Tried a slightly different way.

 

$sql="SHOW COLUMNS FROM `messages`";

$result = mysql_query($sql) or die(mysql_error());
while ($get_info = mysql_fetch_row($result)) {
  foreach ($get_info as $column) {
    echo $column;
  }
}

 

and got

msg_idint(11)NOPRIauto_incrementmsg_tovarchar(99)NOmsg_fromvarchar(99)NOmsg_subjectvarchar(255)NOmsg_bodytextNOread_flgvarchar(3)NOoffsent_datevarchar(255)NO

 

Nearly there but also kind of not.

How do I get shut of the rubbish. I just want the emboldened bit  msg_idint(11)NOPRI

 

Thanks in advance.

<?php
  
include('includes/inc.conn.php');
  
mysql_select_db("discover_messaging");


$result = mysql_query("SHOW COLUMNS FROM `messages`");

if (mysql_num_rows($result) > 0) {
    while ($row = mysql_fetch_assoc($result)) {
        echo $row['Field'];
    }
}
    
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.