Click to See Complete Forum and Search --> : really simple DB query help
rawkman
12-13-2005, 03:20 PM
I'm an ASP guy, not PHP but am starting to lern PHP so forgive the really simple question...
I have a query that returns a bunch of data and I can iterate through and get the data, but I dunno how to get the column names to display. This is the for loop that spits out the data:
for ($i=0; $i <= $fields; $i++)
{
echo "<td>";
echo $array[$i] . " ";
if ( $i < $fields ) echo "</td>";
}
I just want to have the column names in the top row of the table, but not sure how to do it.
bokeh
12-13-2005, 03:45 PM
You will need to run a separate query to get the column names. Something like the following:$table_name = ''; // fill in
$query = "DESC `$table_name`";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result))
{
$fields[] = $row['Field'];
}
// Now you have the column names
// in an array called $fields
Daria
12-13-2005, 03:45 PM
never mind...
chazzy
12-13-2005, 04:13 PM
bokeh:
your solution assumes that the query is particularly of the form "SELECT * FROM `table`"
in general this depends on your database though.
rawkman
12-13-2005, 04:18 PM
yeah, that'll only work if I select on 1 table. I figured out I can do this:
for ($i=0; $i < $fields; $i++)
{
echo "<td>";
echo mysql_field_name($result,$i);
echo $array[$i] . " ";
if ( $i < $fields ) echo "</td>";
}
whereas "echo mysql_field_name($result,$i);" will get me the name of the column. I just can't get it to display in a header once and the then show the rest of the data...maybe 2 while loops will do it...I dunno...
NogDog
12-13-2005, 04:40 PM
$query = "SELECT * FROM `table`";
$result = mysql_query($query) or die(mysql_error());
echo "<table>\n";
$headers = FALSE;
while($row = mysql_fetch_assoc($result))
{
if(!$headers) # if we haven't output the headers, do it now
{
$headers = TRUE; # don't do it again after this
echo "<tr>";
foreach($row as $key => $val)
{
echo "<th>$key</th>";
}
echo "</tr>\n";
}
echo "<tr>"; # output the data row each time
foreach($row as $value)
{
echo "<td>$value</td>";
}
echo "</tr>\n";
}
echo "</table>\n";
bokeh
12-13-2005, 04:41 PM
bokeh:
your solution assumes that the query is particularly of the form "SELECT * FROM `table`"
in general this depends on your database though.To be honest Chazzy the questions on this forum are often so badly written that one would need to be a psychic to know what the poster is really attempting to ask.
rawkman
12-14-2005, 09:46 AM
$query = "SELECT * FROM `table`";
$result = mysql_query($query) or die(mysql_error());
echo "<table>\n";
$headers = FALSE;
while($row = mysql_fetch_assoc($result))
{
if(!$headers) # if we haven't output the headers, do it now
{
$headers = TRUE; # don't do it again after this
echo "<tr>";
foreach($row as $key => $val)
{
echo "<th>$key</th>";
}
echo "</tr>\n";
}
echo "<tr>"; # output the data row each time
foreach($row as $value)
{
echo "<td>$value</td>";
}
echo "</tr>\n";
}
echo "</table>\n";
beauty, thx NogDog, guess you have better interpretation skills than bokeh... (hehe, just kidding bokeh!, after reading the post, I guess I shoulda put I want to pull out the names of the columns from my QUERY not from 1 table)