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" ?>.

5
  • 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. Commented Nov 13, 2016 at 10:35
  • You don't need to put the variable in quotes when you're going to echo it. Commented Nov 13, 2016 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. Commented Nov 13, 2016 at 10:37
  • So what can I do ? @MagnusEriksson Commented Nov 13, 2016 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. Commented Nov 13, 2016 at 10:40

2 Answers 2

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;

Sign up to request clarification or add additional context in comments.

1 Comment

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);
-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;
}

4 Comments

why not, till now it is valid
@ManozBiswas - Since it can't handle Prepared Statements, it is prone to SQL Injection attacks (yes, even if you escape the user input).
Yes, I know pdo or mysqli is better to use nowadays, but your suggestion is not relevant to this question
Doesn't matter. Promoting insecure methods is always bad and should always be pointed out. Specially to people with less experience. That's how people learn.