我试图将数据库中的多行输出到一个数组中,但它只选择最后一行,如何解决这个问题?

$getSkills = $db->query("SELECT * FROM _skills WHERE `chara_id` = '$cid'");
    while ($skill = mysql_fetch_assoc($getSkills)) {
        $sid = $skill['id'];
        $sname = $skill['name'];
        $url = $skill['url'];
    }
    $data = array('skills' => array( '<img id="'.$sid.'" class="skill" src="'.$url.'">','<img id="'.$sid.'" class="skill" src="'.$url.'">','<img id="'.$sid.'" class="skill" src="'.$url.'">','<img id="'.$sid.'" class="skill" src="'.$url.'">' ));
<div class="skill_images"><img id="3" class="skill" src="/img/skills/5.png">

`

最佳答案

您需要在循环中追加到数组。

$data['skills'] = array();
while ($skill = mysql_fetch_assoc($getSkills)) {
     $sid = $skill['id'];
     $sname = $skill['name'];
     $url = $skill['url'];
     $data['skills'][] = '<img id="'.$sid.'" class="skill" src="'.$url.'">';
 }

关于php - 在PHP中输出多个行不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19339257/

10-12 04:43