Forum Moderators: open

Message Too Old, No Replies

Getting and printing field name from mysql

         

cookie2

7:55 pm on Apr 4, 2006 (gmt 0)

10+ Year Member



I'm trying to pull field names from my database and print it with the contents of that field in a specific row. In other words I have this:

ID--Name--Town
0---Tom---Here
1---Harry-There
2---Sally-Everywhere

I want to pull out the info with ID 1 (Harry's) but I want to preface it with the file name it will display like so:

Name: Harry
Town: There

I've been trying to use this:

$query = ("SELECT * FROM `table`);

$result = mysql_query($query);

$nrows = mysql_num_rows($result);
if($nrows!= 0)
{
print "<p>Data for " . $name;
print "<table border=2><tr><th>Info\n";
for($j=0;$j<$nrows;$j++)
{
$row = mysql_fetch_array($result);
print "<tr><td>";
print "$row[ID];<br>";
print "mysql_field_name($result, 1).$row[name];<br>";
print "$row[city];";
print "</td></tr>";
}
print "</table>\n";
}

But it prints out as:

mysql_fetch_field(Resource id #3, 1) Harry

instead of as Name Harry.

How can I fix this?

txbakers

8:07 pm on Apr 4, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hard code the field names in your HTML:

print "Name: " . $row[name]. "<br>";

jatar_k

8:09 pm on Apr 4, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



bad syntax, needs to be changed this way

from
print "mysql_field_name($result, 1).$row[name];<br>";

to
print mysql_field_name($result, 1) . $row[name] . "<br>";

should work

if not you could do it like so

$thefield = mysql_field_name($result, 1);
echo $thefield,' ',$row[name],'<br>';

cookie2

10:58 pm on Apr 4, 2006 (gmt 0)

10+ Year Member



Thank you jatar_k. First suggestion works perfect. Couldn't figure out leaving the first part outside of the quotes. Much obliged.