基于列值打印值 - MYSQL,PHP

时间:2013-05-14 06:43:42

标签: php mysql

如果C2列值为'03',我想将其打印为'K9',如果其值为'O4',那么我想将其打印为'k10',否则任何其他值,它应该显示它。< / p>

我怎么能这样做..

        //my code//
select a.capacity,a.comment, b.c1,b.c2,b.c3 from table1 a inner join table2 b on a.capacity=b.capacity WHERE a.capacity= $name

      $rs = mysql_query( $sql ) or die('Database Error: ' . mysql_error());


        $num = mysql_num_rows( $rs );

        if($num >= 1 ){
            echo "";
            while($row = mysql_fetch_array( $rs )){
        echo "<table>
            <tr>
                <th align='left' valign='middle'>Capacity</th>
                <td align='left' valign='middle'>$row[capacity]</td>
            </tr>
            <tr>
                <th align='left' valign='middle'>C2</th>
               <td align='left' valign='middle'>$row[c2]</td>
                </tr>               
            </table>";
         }

1 个答案:

答案 0 :(得分:0)

尝试使用此while循环替换并检查,它是否适合您:

 while($row = mysql_fetch_array( $rs )){
    $row['c2'] = ( $row['c2'] == '03') ? 'K9' : $row['c2'] ;
    $row['c2'] = ( $row['c2'] == 'O4') ? 'k10' : $row['c2'] ;
    echo "<table>
        <tr>
            <th align='left' valign='middle'>Capacity</th>
            <td align='left' valign='middle'>$row[capacity]</td>
        </tr>
        <tr>
            <th align='left' valign='middle'>C2</th>
           <td align='left' valign='middle'>$row[c2]</td>
            </tr>               
        </table>";
     }