简体   繁体   中英

MySQL only returning one result. Im Baffled

Here is te code:

<?php

//Starting session

session_start();

//Includes mass includes containing all the files needed to execute the full script
//Also shows homepage elements without customs

require_once ('includes/mass.php');

$username = $_SESSION['username'];

if (isset($username))

{   

//Query database for the users networths

$sq_l = "SELECT * FROM user";

$sql_query_worth = mysql_query($sq_l);

while ($row = mysql_fetch_assoc($sql_query_worth))

      {

         $dbusername = $row['username'];


      } 

      echo $dbusername;

}
?>

You're echoing outside of the loop. So it's only echoing the last row. Put that echo statement inside your while loop.

If you want to print all usernames, then the echo statement should be within the while loop, eg.

while ($row = mysql_fetch_assoc($sql_query_worth))
  {
    $dbusername = $row['username'];
    echo $dbusername;
  }

You are using this portion of code :

while ($row = mysql_fetch_assoc($sql_query_worth))
{
     $dbusername = $row['username'];
} 

echo $dbusername;

Which means only the name of the last user will be echo ed :

  • On each iteration of the loop, the username is stored in $dbusername
  • But that variable's content is only echoed aftezr the loop
    • which means only the last value of $row['username'] will be echoed.


If you want to output each usename, you shoud put the echo inside the loop , and not after :

while ($row = mysql_fetch_assoc($sql_query_worth))
{
    $dbusername = $row['username'];
    echo $dbusername . '<br />';
} 

And, with that, each name of each user should be echo ed ;-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM