SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    Join Date
    Feb 2004
    Location
    Netherlands
    Posts
    381
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Get Fieldnames and present the data

    Hey guys,

    Is there a way to first get the fieldnames from a mysql table and then present the data that is in it? (using php)

    ID | Testfield | Message |
    ----------------------------------------
    1 | Bla bla bla | This is the message |

    I want to get this out of it:

    ID : 1
    Testfield : Bla bla bla
    Message: This is the message

    can you guys help me out a bit?

    thanks

  2. #2
    Free your mind Toly's Avatar
    Join Date
    Sep 2001
    Location
    Panama
    Posts
    2,182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $DB_Host "localhost";
    $DB_User "user";
    $DB_Pass "pass";
    $DB_Name "dbase";

    // connect to dbase
    mysql_connect($DB_Host $DB_User $DB_Pass) or die(mysql_error());

    // select dbase
    mysql_select_db($DB_Name) or die(mysql_error());

    // run query
    $result mysql_query("SELECT * FROM table_name") or die(mysql_error());

    // display results
    $row mysql_fetch_array($result);
    echo 
    'ID : '.$row['ID'].'<br>';
    echo 
    'Testfield : '.$row['Testfield'].'<br>';
    echo 
    'Message : '.$row['Message'].'<br>'
    Check out this article for better examples and explantion.
    Community Guidelines | Community FAQ

    "He that is kind is free, though he is a slave;
    he that is evil is a slave, though he be a king." - St. Augustine

  3. #3
    SitePoint Addict
    Join Date
    Feb 2004
    Location
    Netherlands
    Posts
    381
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok sure, but the fieldnames have to be dynamic. So no hardcoding

  4. #4
    Free your mind Toly's Avatar
    Join Date
    Sep 2001
    Location
    Panama
    Posts
    2,182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not quite sure what you mean. Care to explain?
    Community Guidelines | Community FAQ

    "He that is kind is free, though he is a slave;
    he that is evil is a slave, though he be a king." - St. Augustine

  5. #5
    SitePoint Addict
    Join Date
    Feb 2004
    Location
    Netherlands
    Posts
    381
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't know what fieldnames i have in my Database, (want to reuse it) sooo.....

    first the fieldnames have to be picked up. Secondly the data has to be displayed.

    PHP Code:
    echo 'ID : '.$row['ID'].'<br>'
    echo 
    'Testfield : '.$row['Testfield'].'<br>'
    echo 
    'Message : '.$row['Message'].'<br>'
    the $row thing is fine. The hardcoded ID, Testfield and Message are not so fine. This has to be dynamic.

    You understand what I mean?

    cheers

  6. #6
    Free your mind Toly's Avatar
    Join Date
    Sep 2001
    Location
    Panama
    Posts
    2,182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can achieve this with mysql_field_name.

    Here's an example from one of the users' notes from the php website:

    PHP Code:
    <?php
    $result 
    mysql_query("SELECT * FROM table_name") or die(mysql_error());
    $fields mysql_num_fields($result);
    ?>
    <table><tr>
    <?php
    for ($i=0$i mysql_num_fields($result); $i++) {
     
        echo 
    "<th>".mysql_field_name($result$i)."</th>"
    }
    echo 
    "</tr>\n";
        
    while (
    $row mysql_fetch_row($result)) {

        echo 
    "<tr>";
           for (
    $f=0$f $fields$f++) {
               echo 
    "<td>$row[$f]</td>"
           }
        echo 
    "</tr>\n";
    }
    ?>
    </table>
    Community Guidelines | Community FAQ

    "He that is kind is free, though he is a slave;
    he that is evil is a slave, though he be a king." - St. Augustine


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •