0

我有一个数组,它具有从 db 获取的三个值。我没有得到每个值的打印。

$sort_query = "SELECT s_uniqid, mar_tot, mar_per FROM  `exam_record` WHERE  bat=`1314`"
$run_sort = mysql_query($sort_query);

while($selectedvalue = mysql_fetch_assoc($run_sort))
{   

$total[] = $selectedvalue;
}
Print_r($total);

上面的查询给了我结果

Array ( [0] => Array ( [s_uniqid] => 1000017 [mar_tot] => 307 [mar_per] => 61.4 ) [1] => Array ( [s_uniqid] => 1000016 [mar_tot] => 288 [mar_per] => 57.6 ) [2] => Array ( [s_uniqid] => 1000015 [mar_tot] => 279 [mar_per] => 55.8 ) [3] => Array ( [s_uniqid] => 1000009 [mar_tot] => 269 [mar_per] => 53.8 ) [4] => Array ( [s_uniqid] => 1000010 [mar_tot] => 269 [mar_per] => 53.8 )

我想打印 s_uniqid、mar_tot 和 mar_per 的单个值...请建议我。

4

3 回答 3

0

这应该有帮助

foreach($total as $elem) {
    foreach($elem as $key => $val) {
        if($key == 's_uniqid')
            echo 'Unique ID: ' . $val;
        else if($key == 'mar_tot'
            echo 'Total Marks: ' . $val;
    }
}
于 2013-09-02T20:14:31.683 回答
0

像这样的东西可能会奏效:

$s_uniqids, $mar_tots, $mar_pers;
foreach($run_sort as $part) {
  $s_uniqids = $part['s_uniqid'];
  echo "s_uniqid: " .$part['s_uniqid'];

  $mar_tots = $part['mar_tot'];
  echo $mar_tot " .$part['mar_tot'];

  $mar_pers = $part['mar_per'];
  echo "mar_per: " .$part['mar_per'];
}

$s_uniqids,$mar_tots现在$mar_pers是具有提取值的数组。foreach您可以像我之前使用的那样循环遍历它们的各个值。

于 2013-09-02T20:14:42.267 回答
0

警告,使用 MYSQLI!MYSQL 已弃用!!!

$sort_query = "SELECT s_uniqid, mar_tot, mar_per FROM  `exam_record` WHERE  bat=`1314`"
$run_sort = mysqli_query($sort_query);

$suniquid = array();
while($selectedvalue = mysqli_fetch_assoc($run_sort))
{   
   $total[] = $selectedvalue;
   $suniquid[] = $selectedvalue['s_uniqid'];
}
print_r($total);

print_r($suniquid); //Prints values of s_uniqid

编辑:

while($selectedvalue = mysqli_fetch_assoc($run_sort))
{ 
   echo $selectedvalue['s_uniqid'];
   echo $selectedvalue['mar_tot'];
   echo $selectedvalue['amr_per'];
}

例如,如果您有两条记录,这将导致:

s_uniqid
mar_tot
amr_per
s_uniqid
mar_tot
amr_per

否则你可以这样做:

while($selectedvalue = mysqli_fetch_assoc($run_sort))
{
   $unique[] = $selectedvalue['s_uniqid'];
   $martot[] = $selectedvalue['mar_tot'];
   $amrper[] = $selectedvalue['amr_per'];
}

foreach($unique as $val)
{
  echo $val;
}

foreach($martot as $val)
{
  echo $val;
}

foreach($amrper as $val)
{
  echo $val;
}

这将导致(例如再次假设您有两条记录):

s_uniqid
s_uniqid
mar_tot
mar_tot
amr_per
amr_per
于 2013-09-02T20:27:10.030 回答