dtzjvj3915 2014-12-14 23:04
浏览 30
已采纳

如果($ tv =='0'){echo'user'},则根据值更改输出的数据

I'm trying to change the output of a variable from 0 to display user or from 1 to display superuser,

<?php 
    $result = mysql_query("SELECT * FROM users ") or trigger_error(mysql_error());

    $tv = nl2br($row['type']);

    if ($tv == '0') {
        $tv = 'user';
    } elseif ($tv == '3') {
        $tv = 'superuser';                              
    } else {
        $tv = 'N/A';
    }

    while($row = mysql_fetch_array($result)){ 
        foreach($row AS $key => $value) { $row[$key] = stripslashes($value); } 
            echo "<tr>"; 
            echo "<td>" . nl2br( $row['name'] . "</td>";  
            echo "<td>" . nl2br( $row['email']) . "</td>"; 
            echo "<td>" . $tv . "</td>";  
            echo "</tr>";  
    } 
?>

i know I'm validating the data before i do the mysql_fetch_array and that should do it after, but i wasn't able to put the if statement in the echo

any help pls ?

  • 写回答

1条回答 默认 最新

  • drrqwokuz71031449 2014-12-14 23:10
    关注

    You can't access $row before it is set, you can alter your echo line for $row['type'] like this:

    if ($row['type'] == 0) { echo 'user'; } else if ($row['type'] == 1) { echo 'superuser';} else {echo 'NA'; }
    

    <?php 
        $result = mysql_query("SELECT * FROM users ") or trigger_error(mysql_error());
    
        while($row = mysql_fetch_array($result)){ 
            foreach($row AS $key => $value) { $row[$key] = stripslashes($value); } 
                echo "<tr>"; 
                echo "<td>" . nl2br( $row['name'] . "</td>";  
                echo "<td>" . nl2br( $row['email']) . "</td>"; 
                echo "<td>"; if ($row['type'] == 0) { echo 'user'; } else if ($row['type'] == 1) { echo 'superuser';} else {echo 'NA'; } echo "</td>";  
                echo "</tr>";  
        } 
    
    ?>
    

    I'm sure you have a reason for using nl2br, but I totally don't get it.

    As @Zarathuztra has mentionend, please change to mysqli_ or PDO, mysql_ is not the way to go as it is deprecated

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了