Jump to content

how do i echo out the field names from the database?


silverglade

Recommended Posts

hi, i want to echo out 3 fields along with their data.

 

the first field is "About_Widget_corp"

the second field is "Products"

the third field is "Services"

 

data for the fields echoes out with the following code, but i wanted it to be in the following format

 

example.

 

Widget Corp: we make toys

Products: Dolls

Services: Doll Making

 

so far it just outputs , the contents of the fields. any help GREATLY appreciated. thank you. derek

 

here is the code i have so far.

 

<?php
// Five steps to PHP database connections:

// 1. Create a database connection
//		(Use your own servername, username and password if they are different.)
//		$connection allows us to keep refering to this connection after it is established

                              //servername-replace localhost, username-replace root , and that number down there, is password.
$connection = mysql_connect("localhost","root","derek"); 
if (!$connection) {
	die("Database connection failed: " . mysql_error());
}

// 2. Select a database to use 
$db_select = mysql_select_db("widget_corp",$connection); //connect to our database widget_corp.
if (!$db_select) {
	die("Database selection failed: " . mysql_error());
}

?>
<html>
<head>
	<title>Databases</title>
</head>
<body>
<?php
// 3. Perform database query
$result = mysql_query("SELECT * FROM subjects", $connection);
if (!$result) {
	die("Database query failed: " . mysql_error());
}

// 4. Use returned data
while ($row = mysql_fetch_array($result)) {
	echo $row [1] . "<br />" .$row[2]."<br />".$row[3];
	}
  

?>
</body>
</html>
<?php
// 5. Close connection
mysql_close($connection);
?>

 

 

Link to comment
Share on other sites

i tried php.net but found

 

string mysql_field_name  (  resource $result  ,  int $field_offset  )

 

which was just confusing.

 

Well, that will get you the field name from the database - which is not what you want. You want user friendly labels. I hope you noticed I changed mysql_fetch_array() to mysql_fetch_assoc() and am using the field names when referencing the record values. This is much more intuitive in my opinion.

Link to comment
Share on other sites

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.