Jump to content

How to show MYSQL field names in the output?


usman07

Recommended Posts

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

Link to comment
Share on other sites

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";         // ..
?>

Link to comment
Share on other sites

 

 

 

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.

Link to comment
Share on other sites

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>';
}

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.