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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    New Coder
    Join Date
    Aug 2008
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Only One Variable Being Printed

    I've got some code that echoes some data from a table but unless the echo code is within the code that connects to the database (eg. I can't simply echo anywhere on the page, it HAS to be in the same PHP tags as the code that connects/pulls the data) it only shows one line of data. Any reason for this?
    Last edited by iKettles; 08-04-2008 at 11:03 PM.

  • #2
    New Coder
    Join Date
    Jul 2008
    Posts
    99
    Thanks
    0
    Thanked 4 Times in 4 Posts
    Please post your code so we can tell you where the problem is.

    You can echo your DB results anywhere in that PHP file (e.g. inside your html code).

  • #3
    New Coder
    Join Date
    Aug 2008
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by ramm19 View Post
    Please post your code so we can tell you where the problem is.

    You can echo your DB results anywhere in that PHP file (e.g. inside your html code).
    Yeah, that's what I'm trying to do but I can only seem to be able to echo within the code that pulls the data from the database.
    PHP Code:
    <?php
    $username
    ="username";
    $password="password";
    $database="dbname";

    mysql_connect(localhost,$username,$password);
    @
    mysql_select_db($database) or die( "Unable to select database");
    $query="SELECT * FROM newpilots ORDER BY Fullname DESC LIMIT 5";
    $result=mysql_query($query);

    $myrow mysql_fetch_assoc($result);

    do {
         
    $Fullname=$myrow['Fullname'];
         
    $Callsign=$myrow['Callsign'];
         
    $Flag=$myrow['Flag']; 
        
    echo 
    "<table width=200><td>$Callsign - $Fullname</td><td><img src=$Flag></td><tr></tr></table>";


    } while (
    $myrow mysql_fetch_assoc($result)); 
    ?>

  • #4
    New Coder
    Join Date
    Jul 2008
    Posts
    99
    Thanks
    0
    Thanked 4 Times in 4 Posts
    is that code working? post the code containing the html that doesn't work.

    btw, this is how would do it:

    Code:
    <?php
    $username="username";
    $password="password";
    $database="dbname";
    
    mysql_connect(localhost,$username,$password);
    @mysql_select_db($database) or die( "Unable to select database");
    $query="SELECT * FROM newpilots ORDER BY Fullname DESC LIMIT 5";
    $result=mysql_query($query); 
    
    while ($row = mysql_fetch_assoc($result)) {
        echo "<table width=200><td>$myrow['Callsign'] - $myrow['Fullname']</td><td><img src=$myrow['Flag']></td><tr></tr></table>"; 
    }
    
    mysql_free_result($result);
    ?>

  • #5
    New Coder
    Join Date
    Aug 2008
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by ramm19 View Post
    is that code working? post the code containing the html that doesn't work.

    btw, this is how would do it:

    Code:
    <?php
    $username="username";
    $password="password";
    $database="dbname";
    
    mysql_connect(localhost,$username,$password);
    @mysql_select_db($database) or die( "Unable to select database");
    $query="SELECT * FROM newpilots ORDER BY Fullname DESC LIMIT 5";
    $result=mysql_query($query); 
    
    while ($row = mysql_fetch_assoc($result)) {
        echo "<table width=200><td>$myrow['Callsign'] - $myrow['Fullname']</td><td><img src=$myrow['Flag']></td><tr></tr></table>"; 
    }
    
    mysql_free_result($result);
    ?>
    Yeah that's the code with the HTML that works. If I just take out the echo bit and move it to some where else in the page (outside of that code) it will only display one result.

  • #6
    Regular Coder
    Join Date
    May 2008
    Location
    Ohio
    Posts
    231
    Thanks
    3
    Thanked 21 Times in 21 Posts
    PHP Code:
    <?php
    $username
    ="username";
    $password="password";
    $database="dbname";

    mysql_connect(localhost,$username,$password);
    @
    mysql_select_db($database) or die( "Unable to select database");
    $query="SELECT * FROM newpilots ORDER BY Fullname DESC LIMIT 5";
    $result=mysql_query($query);
    echo 
    "<table width=200>"
    while ($myrow mysql_fetch_assoc($result)) {
         echo 
    "<tr>"
         
    $Fullname=$myrow['Fullname'];
         
    $Callsign=$myrow['Callsign'];
         
    $Flag=$myrow['Flag']; 
         echo 
    "<td>$Callsign - $Fullname</td><td><img src=$Flag></td></tr>"
    }
    echo 
    "</table>"
    ?>
    Sorry, I hate do/while syntax

    Honestly, I don't see anything wrong with the code you posted aside from the HTML (which I corrected). It should output at MOST 5 rows (according to your query). Unless there's some minor error in your do/while that I missed... What happens when you run my version?
    zok@zoklet:~$ whereis zok
    zok: http://zoklet.net | http://zoklet.net/otg | /derzok/at/gmail/dot/com

  • #7
    New Coder
    Join Date
    Jul 2008
    Posts
    99
    Thanks
    0
    Thanked 4 Times in 4 Posts
    Yeah that's the code with the HTML that works. If I just take out the echo bit and move it to some where else in the page (outside of that code) it will only display one result.
    that is because you are not using the while loop in your html code...

    post the part of the code that doesn't work and I'll tell you have to do it, it's very simple.

  • #8
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    If you have multiple items displayed, like Full Name, if you want all the full names in the database to show on the page in line, one after the other, then you must put your table inside the while loop.

  • #9
    New Coder
    Join Date
    Aug 2008
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by derzok View Post

    Sorry, I hate do/while syntax

    Honestly, I don't see anything wrong with the code you posted aside from the HTML (which I corrected). It should output at MOST 5 rows (according to your query). Unless there's some minor error in your do/while that I missed... What happens when you run my version?
    Still only displays one result.
    Quote Originally Posted by ramm19 View Post
    that is because you are not using the while loop in your html code...

    post the part of the code that doesn't work and I'll tell you have to do it, it's very simple.
    I have posted the part of the code that doesn't work.

  • #10
    New Coder
    Join Date
    Jul 2008
    Posts
    99
    Thanks
    0
    Thanked 4 Times in 4 Posts
    ic, you HTML is all messed up... use this:

    Code:
    echo "<table width=200>";
    while ($row = mysql_fetch_assoc($result)) {
    echo"  
    <tr>
    <td>$myrow['Callsign'] - $myrow['Fullname']</td>
    <td><img src=\"$myrow['Flag']\"></td>
    </tr>
    ";
    }
    echo "</table>";
    how many rows do you have?

  • #11
    New Coder
    Join Date
    Aug 2008
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by ramm19 View Post
    ic, you HTML is all messed up... use this:

    Code:
    echo "<table width=200>";
    while ($row = mysql_fetch_assoc($result)) {
    echo"  
    <tr>
    <td>$myrow['Callsign'] - $myrow['Fullname']</td>
    <td><img src=\"$myrow['Flag']\"></td>
    </tr>
    ";
    }
    echo "</table>";
    how many rows do you have?
    One row. As for the table tags, they're outside of the PHP code itself.

  • #12
    New Coder
    Join Date
    Jul 2008
    Posts
    99
    Thanks
    0
    Thanked 4 Times in 4 Posts
    put the code I've showed you anywhere you want... of course they have to be inside PHP tags.

    If you have 1 row in your DB it will only show 1 result...

  • #13
    New Coder
    Join Date
    Aug 2008
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by ramm19 View Post
    put the code I've showed you anywhere you want... of course they have to be inside PHP tags.

    If you have 1 row in your DB it will only show 1 result...
    Sorry I read your question wrong, I have 5 rows.
    The problem is nothing to do with the HTML code as just simply echoing the variable with no formatting whatsoever still only shows one result when the echo isn't inside the main PHP tag with the database stuff itself.

  • #14
    Regular Coder
    Join Date
    Jun 2004
    Posts
    565
    Thanks
    0
    Thanked 18 Times in 18 Posts
    You must output the variables within the while, because with each iteration the variables' contents get updated to the values of the next row. When the while is left the variables hold the values of the last row returned.

    Only way to overcome this situation is to store each row in an array, e.g:
    PHP Code:
    $myArray = array();
    while (
    $myrow mysql_fetch_assoc($result)) { 
         
    $myArray[] = $myrow// fill $myArray with the contents of the query result
    }

    // do something

    // output the contents of myArray
    foreach($myArray as $myrow) {
         echo 
    "<tr>" ;
         
    $Fullname=$myrow['Fullname']; 
         
    $Callsign=$myrow['Callsign']; 
         
    $Flag=$myrow['Flag'];  
         echo 
    "<td>$Callsign - $Fullname</td><td><img src=$Flag></td></tr>";

    dumpfi
    Last edited by dumpfi; 08-05-2008 at 09:25 AM.
    "Failure is not an option. It comes bundled with the software."
    ....../)/)..(\__/).(\(\................../)_/)......
    .....(-.-).(='.'=).(-.-)................(o.O)...../<)
    ....(.).(.)("}_("}(.)(.)...............(.)_(.))Ż/.
    ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
    Little did the bunnies suspect that one of them was a psychotic mass murderer with a 6 ft. axe.

  • #15
    New Coder
    Join Date
    Aug 2008
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by dumpfi View Post
    You must output the variables within the while, because with each iteration the variables' contents get updated to the values of the next row. When the while is left the variables hold the values of the last row returned.

    Only way to overcome this situation is to store each row in an array, e.g:
    PHP Code:
    $myArray = array();
    while (
    $myrow mysql_fetch_assoc($result)) { 
         
    $myArray[] = $myrow// fill $myArray with the contents of the query result
    }

    // do something

    // output the contents of myArray
    foreach($myArray as $myrow) {
         echo 
    "<tr>" ;
         
    $Fullname=$myrow['Fullname']; 
         
    $Callsign=$myrow['Callsign']; 
         
    $Flag=$myrow['Flag'];  
         echo 
    "<td>$Callsign - $Fullname</td><td><img src=$Flag></td></tr>";

    dumpfi
    Couldn't seem to be able to get those arrays to work. Is there any disadvantages of them? I did try just including the database.php file and then within the same PHP tags echo it but it didn't want to work. I'm probably just going to write out the code every time because I'm not hugely bothered about a long term fix, I thought it would be much easier than this. Thanks for the help.
    Last edited by iKettles; 08-05-2008 at 12:34 PM.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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