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 4 of 4
  1. #1
    New Coder
    Join Date
    Jan 2008
    Posts
    12
    Thanks
    6
    Thanked 0 Times in 0 Posts

    How can I display more than one column from retreived mysql data?

    When I retrieve data from mysql it automatically lists one row and one column. How can I have it list more than one column in one row?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    You will probably need to provide more information as to what you are doing. We don't know what your query is, the database your using, etc.
    However, I will tell you this, and it will probably solve your problem. Database queries in php usually create a result resource, think of it kinda like an array (its not quite, but just bear with it).
    PHP Code:
    while ($row $qry->fetch_assoc())
    {
        
    // Do what you want on each returned row

    As for the columns, that is likely due to either your query itself only fetching one field, or your data handling of extracting each value. MySQLi fetch_assoc method fetches each field => value pair and stores them into an associative array, so from the above example you can retrive them by doing $row['fieldName'];
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    New Coder
    Join Date
    Jan 2008
    Posts
    12
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Code:
    while ($row = mysql_fetch_assoc($queryresults)) {
    echo $row['image'];
    }
    I want the row image to be in 2 columns when displayed instead of one column.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    You mean for use in a <table>?
    PHP Code:
    <table>
    <?php
    $i 
    0;
    while (
    $row mysql_fetch_assoc($queryresults))
    {
        if (
    $i &#37; 2 == 0)
        
    {
            echo 
    '<tr>';
        }
        echo 
    '<td>';
        echo 
    $row['image'];
        echo 
    '</td>';
        if (
    $i == 1)
        {
            echo 
    '</tr>';
        }
        
    $i++;
    }
    if (
    $i == 1)
    {

        echo 
    '<td></td></tr>';
    }
    ?>
    </table>
    Lesse, will that work? Odd step opens the tr tags, while even ones close it. If it ends in an odd number, close the row. That sounds like it will work. Personally, I'm a css sort of guy, so I'd do something like the following:
    PHP Code:
    $i 0;
    while (
    $row mysql_fetch_assoc($queryresults))
    {
        
    $class '';
        if (
    $i == 1)
        {
            
    $class 'leftFloat';
        }
        if (
    $i == 0)
        {
            
    $class 'rightFloat';
        }
        ... 
    And apply a class to the image instead.
    That looks like it would work, but I haven't tested it out.

    Is that along the idea of what you're looking for?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    javanoobie9 (02-29-2008)


  •  

    Posting Permissions

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