1

I'm using these codes.

$blog = mysql_query("SELECT * FROM blog ORDER BY id");

while($sutun = mysql_fetch_array($blog)) {
    $fake = $sutun["date"];
    echo "$fake"; 
}   

When i use echo"$fake"; I can see all of my rows. But when I use <?php echo "$fake" ?> , It shows just 1 row for me.

I want to all of my rows while I'm using <?php echo "$fake" ?>.

Hans Knöchel
  • 11,422
  • 8
  • 28
  • 49
  • 2
    Don't use the deprecated `mysql_*`-functions. They are deprecated since PHP 5.5 and completely removed in PHP 7. They are also insecure. Use MySQLi or PDO instead. – M. Eriksson Nov 13 '16 at 10:35
  • You don't need to put the variable in quotes when you're going to echo it. – M. Eriksson Nov 13 '16 at 10:36
  • 2
    If you echo the variable after the loop, you will only get the last value, since you're overwriting the variable on each iteration. – M. Eriksson Nov 13 '16 at 10:37
  • So what can I do ? @MagnusEriksson –  Nov 13 '16 at 10:39
  • It depends on what you are going to do with the values after? You might want to build an array with the values to be able to loop through them later, or you might want to append them to the variable (all in one string), depending on what your end game is. – M. Eriksson Nov 13 '16 at 10:40

2 Answers2

0

Beacuse The echo"$fake"; in with in a loop it will echo at every iteration thats why you can see all your rows but <?php echo"$fake"; ?> executed when the loop is done so only the last row will be echoed;

henrybbosa
  • 1,139
  • 13
  • 28
  • If you want to use the variable $fake outside of the while loop, consider using arrays. **$fake[] = $sutun["date"];** and outside of the array, you may use **print_r($fake);** –  Nov 13 '16 at 10:40
-1

You should seperate your logic like

<?php
$blog = mysql_query("SELECT * FROM blog ORDER BY id");

while($sutun = mysql_fetch_array($blog)) {

$fake = $sutun["date"];
?>
<?php
echo $fake;
}
Serjik
  • 10,543
  • 8
  • 61
  • 70
Manoz Biswas
  • 142
  • 1
  • 2
  • 13