usman07 Posted April 14, 2012 Share Posted April 14, 2012 I currently have php code that outputs information from my database, but how do i make it also display the name of the fields beside each entry of the database? heres my PHP code: <?php $server = ""; // Enter your MYSQL server name/address between quotes $username = ""; // Your MYSQL username between quotes $password = ""; // Your MYSQL password between quotes $database = ""; // Your MYSQL database between quotes $con = mysql_connect($server, $username, $password); // Connect to the database if(!$con) { die('Could not connect: ' . mysql_error()); } // If connection failed, stop and display error mysql_select_db($database, $con); // Select database to use // Query database $result = mysql_query("SELECT * FROM Properties"); if (!$result) { echo "Error running query:<br>"; trigger_error(mysql_error()); } elseif(!mysql_num_rows($result)) { // no records found by query. echo "No records found"; } else { $i = 0; echo '<div style="font-family:helvetica; font-size:15px; padding-left:15px; padding-top:20px;">'; while($row = mysql_fetch_array($result)) { // Loop through results $i++; echo '<img class="image1" src="'. $row['images'] .'" />'; //image echo "Displaying record $i<br>\n"; echo "<b>" . $row['id'] . "</b><br>\n"; // Where 'id' is the column/field title in the database echo $row['Location'] . "<br>\n"; // Where 'location' is the column/field title in the database echo $row['Property_type'] . "<br>\n"; // as above echo $row['Number_of_bedrooms'] . "<br>\n"; // .. echo $row['Purchase_type'] . "<br>\n"; // .. echo $row['Price_range'] . "<br>\n"; // .. } echo '</div>'; } mysql_close($con); // Close the connection to the database after results, not before. ?> thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/260920-how-to-show-mysql-field-names-in-the-output/ Share on other sites More sharing options...
JohnOP Posted April 14, 2012 Share Posted April 14, 2012 Whats wrong with just printing the names? echo "Location".$row['Location'] . "<br>\n"; Quote Link to comment https://forums.phpfreaks.com/topic/260920-how-to-show-mysql-field-names-in-the-output/#findComment-1337299 Share on other sites More sharing options...
usman07 Posted April 14, 2012 Author Share Posted April 14, 2012 thanx im just new to php, but yeah that sounds like a great idea. where would i be placing the code? Quote Link to comment https://forums.phpfreaks.com/topic/260920-how-to-show-mysql-field-names-in-the-output/#findComment-1337300 Share on other sites More sharing options...
JohnOP Posted April 14, 2012 Share Posted April 14, 2012 Same place you have it just add the new text beside the row data <?php echo "Location". $row['Location'] . "<br>\n"; // Where 'location' is the column/field title in the database echo "Property". $row['Property_type'] . "<br>\n"; // as above echo "Bedrooms". $row['Number_of_bedrooms'] . "<br>\n"; // .. echo "Purchase". $row['Purchase_type'] . "<br>\n"; // .. echo "Price". $row['Price_range'] . "<br>\n"; // .. ?> Quote Link to comment https://forums.phpfreaks.com/topic/260920-how-to-show-mysql-field-names-in-the-output/#findComment-1337301 Share on other sites More sharing options...
freelance84 Posted April 14, 2012 Share Posted April 14, 2012 You shouldn't really be selecting all from the table anyway. Just select the columns you need.. http://www.sitepoint.com/mysql-mistakes-php-developers/ Go to step 8.. Also, if you are designing the site then you should know the field names anyway.. The only other i way i know of doing this is at command line and DESCRIBE function. Quote Link to comment https://forums.phpfreaks.com/topic/260920-how-to-show-mysql-field-names-in-the-output/#findComment-1337302 Share on other sites More sharing options...
usman07 Posted April 14, 2012 Author Share Posted April 14, 2012 ok thanx, just 1 more, the second and third echo I want to change the font colour just for them, how would I do that because above I have a div style that is set for all of them. { $i = 0; echo '<div style="font-family:helvetica; font-size:15px; padding-left:15px; padding-top:20px;">'; while($row = mysql_fetch_array($result)) { // Loop through results $i++; echo '<img class="image1" src="'. $row['images'] .'" />'; //image echo "Displaying record $i<br>\n"; echo "<b>" . $row['id'] . "</b><br>\n"; // Where 'id' is the column/field title in the database echo "Location: ". $row['Location'] . "<br>\n"; // Where 'location' is the column/field title in the database echo "Property Type: ". $row['Property_type'] . "<br>\n"; // as above echo "Bedrooms: ". $row['Number_of_bedrooms'] . "<br>\n"; // .. echo "Purchase Type: ". $row['Purchase_type'] . "<br>\n"; // .. echo "Price: ". $row['Price_range'] . "<br>\n"; // .. } echo '</div>'; } Quote Link to comment https://forums.phpfreaks.com/topic/260920-how-to-show-mysql-field-names-in-the-output/#findComment-1337304 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.