Connecting Tech Pros Worldwide Forums | Help | Site Map

PHP/MySQL Query about tables

JV
Guest
 
Posts: n/a
#1: Jul 17 '05
k heres the skinny server =
php v.4.3.8 w/ MySQL support
linux box
working apache 2.0 server

when connection to the database and submitting a query with
<?
$result = mysql_query($query) or die("blah blah");
echo "<table>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)){
echo "\t<tr>\n";
foreach ($line as $col_value){
echo ("\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
}
?>

the appropriate records are returned...
but my question is:
Is there any way to extract the column names from $result???

thnx for help in advance

to respond via email switch the @ and the . in email addr



Ian.H
Guest
 
Posts: n/a
#2: Jul 17 '05

re: PHP/MySQL Query about tables


On Fri, 03 Sep 2004 03:15:32 GMT, "JV" <jveil.hotpop@com> wrote:
[color=blue]
>k heres the skinny server =
> php v.4.3.8 w/ MySQL support
> linux box
> working apache 2.0 server
>
>when connection to the database and submitting a query with[/color]


[ snip code ]
[color=blue]
>the appropriate records are returned...
>but my question is:
>Is there any way to extract the column names from $result???
>
>thnx for help in advance[/color]


Have a look at
<http://uk.php.net/manual/en/function.mysql-field-name.php>


Should be what you're after =)


HTH.



Regards,

Ian

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/
JV
Guest
 
Posts: n/a
#3: Jul 17 '05

re: PHP/MySQL Query about tables



"Ian.H" <ian@WINDOZEdigiserv.net> wrote in message
news:urofj0pauo8c6jj3i00jvdd0bhlmqhcjm5@4ax.com...[color=blue]
> Have a look at
> <http://uk.php.net/manual/en/function.mysql-field-name.php>
>
>
> Should be what you're after =)
>
>
> HTH.
>
>
>
> Regards,
>
> Ian
>
> --
> Ian.H
> digiServ Network
> London, UK
> http://digiserv.net/[/color]

thnx ian for timely response

k looked at it and a couple questions arises ?
1. is there a variable stored in $result that will tell how may columns are
in it ?
and depending on answer from that...
2. where in loop structure would be best place to put it (obviously at the
top of loop right after the <table> flag so i can use <th> flags to display
the headers but it seems like it might interfere with the existing $result
variable

thnx again
JV


Ian.H
Guest
 
Posts: n/a
#4: Jul 17 '05

re: PHP/MySQL Query about tables


On Fri, 03 Sep 2004 03:36:20 GMT, "JV" <jveil.hotpop@com> wrote:


Hi JV..

[color=blue]
>
>"Ian.H" <ian@WINDOZEdigiserv.net> wrote in message
>news:urofj0pauo8c6jj3i00jvdd0bhlmqhcjm5@4ax.com.. .[color=green]
>> Have a look at
>> <http://uk.php.net/manual/en/function.mysql-field-name.php>
>>
>>
>> Should be what you're after =)
>>
>>
>> HTH.[/color][/color]
[color=blue]
>
>thnx ian for timely response[/color]


np =)

[color=blue]
>k looked at it and a couple questions arises ?
>1. is there a variable stored in $result that will tell how may columns are
>in it ?[/color]


<http://uk.php.net/manual/en/function.mysql-num-fields.php>


echo mysql_num_fields($result);

[color=blue]
>and depending on answer from that...
>2. where in loop structure would be best place to put it (obviously at the
>top of loop right after the <table> flag so i can use <th> flags to display
>the headers but it seems like it might interfere with the existing $result
>variable[/color]


-----------------
[! UNTESTED CODE !]
-----------------

$result = mysql_query("
SELECT *
FROM " . TABLE_NAME . "
");

if (is_resource($result)) {
echo "<table>\n<tr>";
for ($i = 0; $i < mysql_num_fields($result); $i++) {
echo '<th>' . mysql_field_name($result, $i) . '</th>';
}
echo "</tr>\n";

if (mysql_num_rows($result) > 0) {
if ($row = mysql_fetch_assoc($result)) {
do {
echo "<tr>\n";
for ($i = 0; $i < mysql_num_fields($result); $i++) {
echo '<td>' . $row[mysql_num_fields($result, $i)] . "</td>\n";
}
echo "</tr>\n";
}
while ($row = mysql_fetch_assoc($result));
}

mysql_free_result($result);
}
} else {
die(mysql_error());
}

[color=blue]
>
>thnx again
>JV
>[/color]



HTH (and works) =)



Regards,

Ian

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/
Tony Marston
Guest
 
Posts: n/a
#5: Jul 17 '05

re: PHP/MySQL Query about tables


If you want the column names as well as their values then try this:

foreach ($line as $col_name => $col_value) {

Easy peasy lemon squeezy.

--
Tony Marston

http://www.tonymarston.net



"JV" <jveil.hotpop@com> wrote in message
news:nlRZc.280257$eM2.212438@attbi_s51...[color=blue]
>k heres the skinny server =
> php v.4.3.8 w/ MySQL support
> linux box
> working apache 2.0 server
>
> when connection to the database and submitting a query with
> <?
> $result = mysql_query($query) or die("blah blah");
> echo "<table>\n";
> while ($line = mysql_fetch_array($result, MYSQL_ASSOC)){
> echo "\t<tr>\n";
> foreach ($line as $col_value){
> echo ("\t\t<td>$col_value</td>\n";
> }
> echo "\t</tr>\n";
> }
> ?>
>
> the appropriate records are returned...
> but my question is:
> Is there any way to extract the column names from $result???
>
> thnx for help in advance
>
> to respond via email switch the @ and the . in email addr
>
>[/color]


Closed Thread