Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    display mysql table

    Hello. I have a MySQL table that has three columns per row ID, title, and body. How do I display it like this:

    Code:
    row 1:
    <div>
     <div>title</div>
     <div>body</div>
    </div>
    
    row 2:
    <div>
     <div>title</div>
     <div>body</div>
    </div>
    
    and so on.

  • #2
    Regular Coder
    Join Date
    Sep 2005
    Posts
    394
    Thanks
    1
    Thanked 0 Times in 0 Posts
    PHP Code:
    <?php
    mysql_connect
    ('localhost''username''password');
    mysql_select_db('database_name');
    $result mysql_query("SELECT `title`, `body` FROM `table_name`");
    while (
    $row mysql_fetch_array($result)) { 
       echo 
    '
          <div>
             <div>' 
    $row['title'] . '</div>
             <div>' 
    $row['body'] . '</div>
          </div>
       '
    ;
    }
    ?>
    ~Phil~

  • #3
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks allot. one more question, how can I have it so when a link by the the row is clicked the row is deleted from the table?

  • #4
    Regular Coder
    Join Date
    Sep 2005
    Posts
    394
    Thanks
    1
    Thanked 0 Times in 0 Posts
    PHP Code:
    <?php 
    if (!isset($_GET['did'])) {
       
    mysql_connect('localhost''username''password'); 
       
    mysql_select_db('database_name'); 
       
    $result mysql_query("SELECT `ID`, `title`, `body` FROM `table_name`"); 
       while (
    $row mysql_fetch_array($result)) {  
          echo 

             <div> 
                <div>' 
    $row['title'] . '</div> 
                <div>' 
    $row['body'] . '</div>
                <div><a href="' 
    $_SERVER['PHP_SELF'] . '?did=' $row['ID'] . '">Delete</a></div> 
             </div> 
          '

       } 
    }
    else {
       
    $id mysql_real_escape_string($_GET['id']);
       
    mysql_query("DELETE FROM `table_name` WHERE `ID`='$id'");
       
    header("Location: " $_SERVER['PHP_SELF']);
    }
    ?>
    ~Phil~

  • #5
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thank you very very much.

  • #6
    Regular Coder
    Join Date
    Sep 2005
    Posts
    394
    Thanks
    1
    Thanked 0 Times in 0 Posts
    welcome

  • #7
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    im little confused shouldin't the delete code be like this:

    PHP Code:
    if (!isset($_GET['did']))
    {
         
    $id mysql_real_escape_string($_GET['did']);
         
    mysql_query("DELETE FROM `test_table` WHERE `id`='$id'");
         
    header("Location:"$_SERVER['PHP_SELF']);
    }
    else
    {  
         
    $result mysql_query("SELECT `id`, `title`, `body` FROM `test_table`"); 
         while (
    $row mysql_fetch_array($result))
         {
              echo 
    '
              <div>
                   <div>'
    .$row['title'].'</div>
                   <div>'
    .$row['body'].'</div>
                   <div><a href="'
    .$_SERVER['PHP_SELF'].'?did='.$row['id'].'">Delete</a></div>
              </div>
              '
    ;
         }

    Last edited by mike_m; 06-11-2006 at 05:37 AM.

  • #8
    Regular Coder
    Join Date
    Sep 2005
    Posts
    394
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Simple answer, no. Mine was the correct way around. Why would you do this...

    PHP Code:
    if (!isset($_GET['did'])) 

         
    $id mysql_real_escape_string($_GET['did']); 
    ??? You are basically saying if $_GET['did'] isn't set, set $id = to it. Therefore, $id is always = to NULL.

    ~Phil~

  • #9
    Regular Coder
    Join Date
    Sep 2005
    Posts
    394
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Although, I did make a mistake there...

    PHP Code:
    <?php  
    mysql_connect
    ('localhost''username''password');  
    mysql_select_db('database_name');

    if (!isset(
    $_GET['did'])) { 
       
    $result mysql_query("SELECT `ID`, `title`, `body` FROM `table_name`");  
       while (
    $row mysql_fetch_array($result)) {   
          echo 
    '  
             <div>  
                <div>' 
    $row['title'] . '</div>  
                <div>' 
    $row['body'] . '</div> 
                <div><a href="' 
    $_SERVER['PHP_SELF'] . '?did=' $row['ID'] . '">Delete</a></div>  
             </div>  
          '
    ;  
       }  

    else { 
       
    $id mysql_real_escape_string($_GET['id']); 
       
    mysql_query("DELETE FROM `table_name` WHERE `ID`='$id'"); 
       
    header("Location: " $_SERVER['PHP_SELF']); 

    ?>
    You were only connecting to the database server when $_GET['did'] wasn't set.

  • #10
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    well im doing somthing else with it. with a url like ths:
    Code:
    mysite.com/admin.php?id=01&action=remove
    the code above was just some late night confusion


  •  

    Posting Permissions

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