在php mysql中回显while语句外的多个字段

在php mysql中回显while语句外的多个字段

问题描述:

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.

很抱歉,因为我是初学者看起来很傻 p>

我正在比较id的 并显示数据。 p>

预期输出 strong> p>

Name1 Name2 Name3 p> blockquote> \ n

代码。 strong> p>

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

但是,我想在循环 code>的之外 echo code>,所以我尝试了以下但是 它只输出最后一个值,即Name3 p>

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

我能理解 loop code>将在while循环后终止,因此它不会执行所需的循环。 但是,我认为可能有一个解决方法。 任何人都可以帮助我。 p> div>

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;

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