使用php将数据库中的记录返回到表中

时间:2015-09-22 13:37:03

标签: php mysql record

我有这个脚本,我第一次将记录返回到表中。它工作得很好,但它只返回最后添加的记录。你能看一下吗,因为我找不到什么问题。

 $query = 'SELECT character_id, alias, real_name, alignment
        FROM comic_character
        ORDER BY ' . $order[$o];
        $result = mysql_query($query, $db) or die (mysql_error($db));  

if (mysql_num_rows($result) > 0) {
echo '<table>';
echo '<tr><th><a href="' . $_SERVER['PHP_SELF'] . '?o=1">Alias</a></th>';
echo '<th><a href="' . $_SERVER['PHP_SELF'] . '?o=2">Real Name</a></th>';
echo '<th><a href="' . $_SERVER['PHP_SELF'] . '?o=3">Alignment</a></th>';
echo '<th>Powers</th>';
echo '<th>Enemies</th></tr>';

$odd = true;   
while ($row = mysql_fetch_array($result)) {
       echo ($odd == true) ? '<tr class="odd_row">' : '<tr class="even_row">';
       $odd = !$odd;
       echo '</td><td><a href="edit_character.php?id=' . $row['character_id'] . '">' . $row['alias'] . '</a></td>';
       echo '<td>' . $row['real_name'] . '</td>';
       echo '<td>' . $row['alignment'] . '</td>';

  $query2 = 'SELECT  power FROM comic_power p
                   JOIN comic_character_power cp
                   ON p.power_id = cp.power_id
                   WHERE cp.character_id = ' . $row['character_id'] . '
                   ORDER BY power ASC';
$result2 = mysql_query($query2, $db) or die(mysql_error($db));

     if (mysql_num_rows($result2) > 0) {
      $powers = array();
      while ($row2 = mysql_fetch_assoc($result2)) {
      $powers[] = $row2['power'];
      }
      echo '<td>' . implode(',', $powers) . '</td>';
     } else {
     echo '<td>none</td>';

0 个答案:

没有答案