0

Sorry if look silly as i am a beginner

I am comparing id's and displaying data .

Expected output

Name1 Name2 Name3

Code.

$cout = mysql_query("SELECT * FROM student where parent_id='$update_id' ",$link);
while($cput = mysql_fetch_array($cout)){
  echo $cput['s_name'];   
}

But, i want to echo outside while loop, so i tried the following but it outputs only the last value i.e., Name3

$cout = mysql_query("SELECT * FROM student where parent_id='$update_id' ",$link);
while($cput = mysql_fetch_array($cout)){
  $sname = $cput['s_name'];
}
echo $sname;

I can understand that the loop will terminate after while loop and hence it won't do the required one . But, i think that there might be a work around . Anyone can help me over here please.

Mihriban Minaz
  • 3,043
  • 2
  • 32
  • 52
Raja Gopal
  • 1,845
  • 1
  • 17
  • 34
  • "*i want to echo outside while loop*", why? – D4V1D Apr 04 '16 at 13:45
  • 2
    [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Apr 04 '16 at 13:47
  • 3
    Do appending using concatenation operator like as `$sname .= $cput['s_name'];` – Narendrasingh Sisodia Apr 04 '16 at 13:47
  • 2
    Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Apr 04 '16 at 13:47
  • @D4V1D i am using the ['s_name'] at several places .so i thought instead of using the loop again and again i can try a work around mate . – Raja Gopal Apr 04 '16 at 13:48
  • 1
    @Uchiha That worked , thanks a lot mate – Raja Gopal Apr 04 '16 at 13:52
  • 1
    You're Welcome. Glad it worked for you. But please take @JayBlanchard comment seriously. – Narendrasingh Sisodia Apr 04 '16 at 13:53
  • @JayBlanchard Sure mate , i will take your comments seriously and will act upon in the future . i am a beginner . just learning things and learning from errors – Raja Gopal Apr 04 '16 at 14:07

2 Answers2

3

While using a concatenated variable works, note the trailing space that will ultimately be outputted. Or imagine you wanted to separate the names with commas. The aforementioned code - with commas - would output:

$cout = mysql_query("SELECT * FROM student where parent_id='$update_id' ",$link);
$sname = '';
while($cput = mysql_fetch_array($cout)){
    $sname .= $cput['s_name'] . ', ';
}
echo $sname; // Name1, Name2, Name3, // note trailing (unwanted) comma with *space*

An alternative to this would be to capture the output in an array. This would allow you to then implode() whatever delimiter you so please. Consider the following:

$cout = mysql_query("SELECT * FROM student where parent_id='$update_id' ",$link);
$sname = [];
while($cput = mysql_fetch_array($cout)){
    $sname[] = $cput['s_name'];
}
echo implode(' ', $sname); // Name1 Name2 Name3
echo implode(', ', $sname); // Name1, Name2, Name3
echo implode('<br/>', $sname); // Name1<br/>Name2<br/>Name3
// etc.

UPDATE

$cout = mysql_query("SELECT * FROM student where parent_id='$update_id' ",$link);
echo '<select name="mySelectBox">';
while($cput = mysql_fetch_array($cout)){
    echo '<option value="Some Value">' . $cput['s_name'] . '</option>';
}
echo '</select>';
mferly
  • 1,646
  • 1
  • 13
  • 19
  • This answer is really informative and thanks a lot . i gained some idea form your answer mate . Thanks – Raja Gopal Apr 04 '16 at 14:12
  • Agreed, not having to care about what exactly to do or watch out for when you can just add those items to an array and implode it afterwards is the best way to go. Leave silly little details to specialist functions that care, but don't litter your general code with these things. – klaar Apr 04 '16 at 14:15
  • @Marcus can you guide me how to implode to select box – Raja Gopal Apr 05 '16 at 05:35
  • 1
    @RajaGopal implode() wouldn't be the best-case for wrapping with . What are you looking to accomplish? See update above. – mferly Apr 05 '16 at 11:48
2

Define a variable to keep names with space separated ($sname) then concatenate s_name value in each iteration like $sname .= $cput['s_name'] . ' ';

So your code will be like...

$cout = mysql_query("SELECT * FROM student where parent_id='$update_id' ",$link);
$sname = '';
while($cput = mysql_fetch_array($cout)){
    $sname .= $cput['s_name'] . ' ';
}
echo $sname;
tanaydin
  • 5,171
  • 28
  • 45