silverglade Posted April 27, 2010 Share Posted April 27, 2010 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 https://forums.phpfreaks.com/topic/199974-how-do-i-echo-out-the-field-names-from-the-database/ Share on other sites More sharing options...
Psycho Posted April 27, 2010 Share Posted April 27, 2010 // 4. Use returned data while ($row = mysql_fetch_assoc($result)) { echo "Widget Corp: {$row['About_Widget_corp']}<br />\n"; echo "Products: {$row['Products']}<br />\n"; echo "Services: {$row['Services']}<br />\n"; } Link to comment https://forums.phpfreaks.com/topic/199974-how-do-i-echo-out-the-field-names-from-the-database/#findComment-1049587 Share on other sites More sharing options...
silverglade Posted April 27, 2010 Author Share Posted April 27, 2010 awesome! thank you mjdamato. that works perfectly. great thank you. . i tried php.net but found string mysql_field_name ( resource $result , int $field_offset ) which was just confusing. so thank you. derek Link to comment https://forums.phpfreaks.com/topic/199974-how-do-i-echo-out-the-field-names-from-the-database/#findComment-1049590 Share on other sites More sharing options...
Psycho Posted April 27, 2010 Share Posted April 27, 2010 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 https://forums.phpfreaks.com/topic/199974-how-do-i-echo-out-the-field-names-from-the-database/#findComment-1049598 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.