-3

Hello please help me is this code this code only show me last row of loop not full.

$sql = mysql_query("SELECT hash FROM users");
while($row=mysql_fetch_assoc($sql)) {
    $url = $row["hash"]."<br />";

}

5b50373f0dbd8ab7fe888060257967f2e0adcbdb

but full list is:

a065e7094234b1c836f9829ca9185a36132ff76f 5b50373f0dbd8ab7fe888060257967f2e0adcbdb

pleas help me

chris85
  • 23,846
  • 7
  • 34
  • 51
Ammad Khalid
  • 176
  • 2
  • 9
  • Make `$url` an array, or concatenate it with a new line... – chris85 Aug 13 '15 at 18:32
  • 1
    Or `$url .= $row["hash"]."
    ";`
    – JungleZombie Aug 13 '15 at 18:38
  • 1
    If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Aug 13 '15 at 18:59
  • 1
    Any issues or questions with either of the provided answers? http://stackoverflow.com/help/someone-answers – chris85 Aug 13 '15 at 19:46
  • You need to explain the problem a bit more, maybe show the table structure. – Criesto Sep 12 '15 at 03:37

2 Answers2

2

You can accomplish this one of three ways. Currently you are resetting $url on every iteration.

$sql = mysql_query("SELECT hash FROM users");
$url = '';
while($row=mysql_fetch_assoc($sql)) {
    $url .= $row["hash"] . "<br />";

}

or

$sql = mysql_query("SELECT hash FROM users");
$url = '';
while($row=mysql_fetch_assoc($sql)) {
    echo $row["hash"] . "<br />";

}

or

$sql = mysql_query("SELECT hash FROM users");
while($row=mysql_fetch_assoc($sql)) {
    $url[] = $row["hash"] . "<br />";
}

With this approach you need to iterate through the array later, foreach..

chris85
  • 23,846
  • 7
  • 34
  • 51
  • 1
    I have been racking my brain (for about an hour now) on something I'm working on in order to be able to echo out more than one row "outside" a loop where I couldn't "echo" inside a loop, since I have about a dozen different sections in a bootstrapped site that requires echoing just a variable and not a whole bunch of "in and out" PHP loops. Your first suggestion was the one that worked for me Chris; the others did not. I'll be keeping this one "on file". *Cheers* – Funk Forty Niner Sep 06 '16 at 16:39
  • 1
    @Fred-ii- Glad someone was able to use it. – chris85 Sep 06 '16 at 17:49
  • It has Chris; for me anyway and "thanks" for having submitted the answer that may very well help others also. I'm sure the OP got their solution but just ran off. It's not like they haven't been told what to do with the answers given neither. *Cheers* – Funk Forty Niner Sep 06 '16 at 18:53
0

This is a good use case for foreach:

<?php
$sql = mysql_query("SELECT hash FROM users");
$rows = mysql_fetch_assoc($sql);
foreach ($rows as $row) {
    $url = $row["hash"]."<br />";
}
?>

Plus, you're overwriting the $url variable each iteration. Either push each value onto an array, or concatenate a string:

<?php
// ...
$urls = '';
$url_list = array();
foreach ($rows as $row) {
    $urls .= $row['hash'].'<br>'; // concatenation
    $url_list[] = $row['hash']; // add each URL to an array
}
?>

Then you can either output the final string, or iterate over the array when you want to recall the values.

Jeff
  • 789
  • 4
  • 13
  • Why? Doesn't `while` work just as well in this case? You're still overwriting `$url` each time. – Jay Blanchard Aug 13 '15 at 18:59
  • Oh. Oops. Your problem is that you're overwriting the `$url` variable each time. You should either use concatenation (`$url .= $row["hash"];`) or add them to an array (`$url_list[] = $row["hash"];`). – Jeff Aug 13 '15 at 19:03
  • You still haven't said why `foreach` is a good use case here. – Jay Blanchard Aug 13 '15 at 19:06
  • I prefer it for style reasons since I find the `while` syntax a bit weird. – Jeff Aug 13 '15 at 19:06
  • That's cool, but if you're going to say something to the effect of *"This is a good use case for..."* you need to explain why. – Jay Blanchard Aug 13 '15 at 19:09