MySQL sadece bir sonuç geri döndürülmesi.

3 Cevap php

İşte te kodu:

<?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;

}
?>

3 Cevap

Sen döngünün dışında yankılanan ediyoruz. Yani sadece son satırı yankılanan oluyor. Sizin ise döngü içinde bu echo deyimi koydu.

Tüm adlarını yazdırmak istiyorsanız, o zaman echo deyimi örneğin, while döngüsü içinde olmalıdır.

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

Bu kod bölümünü kullanıyor:

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

echo $dbusername;

Anlamına gelir only the name of the last user will be echo ed :

  • Döngünün her tekrarında, adı $dbusername saklanır
  • But that variable's content is only echoed aftezr the loop
    • Hangi $row['username'] yankılandı olacak sadece son değer anlamına gelir.


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 />';
} 

Ve bu, her kullanıcının her isim echo ed olmalıdır ;-)