Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我在一个页面中有两个PHP代码块,但只有第一个运行_Php - Fatal编程技术网

我在一个页面中有两个PHP代码块,但只有第一个运行

我在一个页面中有两个PHP代码块,但只有第一个运行,php,Php,我有以下两段PHP代码,都在同一个页面上,但只有第一段运行。有什么问题吗 守则: 首先,确保正确打开和关闭HTML标记。然后将代码更改为以下内容: <?php $username = null; $bash = null; if ($result -> num_rows > 0) { $row = $result->fetch_assoc(); $username = $row["username"];

我有以下两段PHP代码,都在同一个页面上,但只有第一段运行。有什么问题吗

守则:

首先,确保正确打开和关闭HTML标记。然后将代码更改为以下内容:

<?php
    $username = null;
    $bash = null;
    if ($result -> num_rows > 0) {
        $row = $result->fetch_assoc();
        $username = $row["username"];
        $bash = $row["bash"];
    }
?>


<div class = "row" >
    <div class = "col-md-12">
        enter code here
        <h1 id = "username" style = "color:white; text-align:center">
            <?= $username ?> <!-- OR --> <?php echo $username; ?>
        </h1>
    </div>
</div>


<div class = "row">
    <div class = "col-md-12">
        <h1 id = "bash" style = "color:blue; text-align:center">
            <?= $bash ?> <!-- OR --> <?php echo $bash; ?>
        </h1>
    </div>
</div>
当您可以只执行一次PHP代码块时,绝对没有理由重复相同的PHP代码块两次。 您可以在条件语句之外启动两个变量,然后根据结果更改它们的值。 然后,您可以只回显h1标记中的变量,然后分别返回。
旁注:要在尝试在没有if的情况下在第二个PHP部分中回显测试,以检查您的假设是否正确。由于您没有提供有关该问题的足够信息,很可能该问题将被解决。是否启用了错误报告?若否,原因为何?如果是,是否显示任何错误?可能是查询错误吗?是否已尝试调试或转储查询数据?告诉我们您正在尝试做什么和正在发生什么。每次运行$result->fetch\u assoc时,您都会推进光标$行[bash]是第2行的bash。你的问题不是很清楚,所以这只是一个猜测。你意识到,通过运行两次fetch_assoc,你正在尝试获取两个不同的行,对吗?@webdeb its works fineProTip:你可以使用echo而不是使用冗长的方法。这是我最喜欢的方法/意见。这是一种我不知道的非常有用的方法。非常感谢@Script47。我将编辑答案。注意,要在PHP中工作,需要启用短标记。@AngelPolitis,非常感谢,您的答案解决了我的问题。。你是最棒的,谢谢大家的帮助
<?php
    $username = null;
    $bash = null;
    if ($result -> num_rows > 0) {
        $row = $result->fetch_assoc();
        $username = $row["username"];
        $bash = $row["bash"];
    }
?>


<div class = "row" >
    <div class = "col-md-12">
        enter code here
        <h1 id = "username" style = "color:white; text-align:center">
            <?= $username ?> <!-- OR --> <?php echo $username; ?>
        </h1>
    </div>
</div>


<div class = "row">
    <div class = "col-md-12">
        <h1 id = "bash" style = "color:blue; text-align:center">
            <?= $bash ?> <!-- OR --> <?php echo $bash; ?>
        </h1>
    </div>
</div>