简体   繁体   English

变量不会显示

[英]A variable won't display

I have code that runs a query on the database and returns some data and displays it. 我有在数据库上运行查询并返回一些数据并显示它的代码。 All very simple. 一切都很简单。 I've tested the query and it work's perfectly. 我已经测试过该查询,并且工作正常。

So somewhere between the query being executed and me displaying the data, it's not working. 因此,在执行查询和显示数据之间的某个地方,它不起作用。

$q = "SELECT u.username, r.position, r.score, r.winner, t.team FROM ".TBL_FOOT_TOUR_ROUNDS." r
                      LEFT JOIN ".TBL_USERS." u ON u.id = r.winner
                      LEFT JOIN ".TBL_FOOT_TOUR_PLAYERS." pl ON pl.userid = r.winner
                      LEFT JOIN ".TBL_FOOT_TEAMS." t ON t.id = pl.team
                      WHERE r.tourid = '$tour_id' && r.round = '$i' ORDER BY r.position";
                $result = $database->query($q);
                ?>
                <div class="vertical-holder">
                    <div class="vertical-header"><p><?php echo $roundName[$i]; ?></p></div>
                    <?php
                    while($row=mysql_fetch_assoc($result)){
                        extract($row);
                        if($k&1){
                            ?>
                            <div class="horizontal-holder<?php echo $j; ?>">
                                <div class="white-holder">
                                    <div class="player-holder">
                                        <?php
                                        if($winner == 0){
                                            echo "<p>TBC</p>";
                                        }
                                        else{
                                            echo "<p><a href='/profile.php?id=$winner'>$username</a><br />$team</p>";
                                        }
                                        ?>
                                    </div>
                                    <div class="score-holder">
                                        <?php 
                                        if($score == NULL){
                                            echo "<p>-</p>";
                                        }
                                        else{
                                            echo "<p>$score</p>";
                                        }
                                        ?>
                                    </div>
                                </div>
                            <?php
                        }

That's the snippet of code that's (what I believe to be) relevant. 这是(我认为是相关的)代码片段。

The score is showing as '-' all the time even when a score is present. 即使存在分数,分数也始终显示为“-”。

The rest of the returned data shows no problem. 其余的返回数据显示没有问题。

Can anyone see why the score variable isn't showing? 谁能看到为什么分数变量没有显示?

Thanks 谢谢

<?php 
    if($score == NULL){
        echo "<p>-</p>";
    } else {
        echo "<p>$score</p>";
    }
?>

you got it wrong. 你理解错了。 if the value in the database is null, $score will not be null, it will be the string "null". 如果数据库中的值为null,则$ score将不为null,它将为字符串“ null”。 so try 所以尝试

<?php 
    if(strtoupper($score) === "NULL"){
        echo "<p>-</p>";
    } else {
        echo "<p>$score</p>";
    }
?>

:) :)

alternatively you can create some utility function that changes a variable: 或者,您可以创建一些实用程序函数来更改变量:

function nullify(&$data) {
    if(strtoupper($data) === "NULL") {
        $data = NULL;
    }
}

and then call it like this: 然后这样称呼它:

nullify($score);

if $score should be set to null, it will be null after the call. 如果$ score应该设置为null,则在调用后它将为null。 then you can keep your logic the way it is ^^ 那么您可以按照自己的方式保持逻辑^^

Not sure what $score contains, but it seems to me that you could write your test like: 不知道$score包含什么,但是在我看来,您可以像这样编写测试:

if($score < -1){ // or $score <= -1
    echo "<p>-</p>";
}
else{
    echo "<p>$score</p>";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM