php 解析错误:语法错误,意外的“else”(T_ELSE)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21075325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:48:14  来源:igfitidea点击:

Parse error: syntax error, unexpected 'else' (T_ELSE)

php

提问by user3164987

I edit this code to display a mesaje when select is null and i get this error:

我编辑此代码以在 select 为 null 时显示 mesaje 并且出现此错误:

 Parse error: syntax error, unexpected 'else' (T_ELSE) in /nginx/user/reports.php on line 235

This is code:

这是代码:

 $stmt = $mysqli->prepare("SELECT date, impressions, balance, username FROM reports WHERE username = '$username' and date between '$firstDay' AND '$lastDay'");
    $stmt->execute();
    $stmt->store_result();
    $rows = $stmt->num_rows;
    mysqli_stmt_bind_result($stmt,$date,$impressions,$balance,$username);
       if ($rows > 0){
       while (mysqli_stmt_fetch($stmt)) {   
           $ecpm_t3 = $balance*1000;
       $ecpm3 = $ecpm_t3/$impressions;
    $rate = number_format((float)$ecpm3, 2, '.', '');
     echo"<tr>
            <td class=\"nn\" style=\"text-align:center;\" align=center>$date</td>
            <td class=\"nn\" style=\"text-align:center;\" align=center>$impressions</td>
            <td class=\"nn\" style=\"text-align:center;\" align=center>$$rate</td>
            <td class=\"nn\" style=\"text-align:center;\" align=center nowrap>$$balance</td>
        </tr>";
       } else {
        echo"<tr>
            <td colspan=\"4\" class=\"nn\" style=\"text-align:center;\" align=center>Not rows</td>
        </tr>";
    }
        }

And line 235 is:

第 235 行是:

   } else {

How i can resolve this error ? What is bad ?

我该如何解决这个错误?什么是坏?

回答by Ilia Rostovtsev

Working example (fixed and formatted):

工作示例(固定和格式化):

$stmt = $mysqli->prepare("SELECT date, impressions, balance, username FROM reports WHERE username = '$username' and date between '$firstDay' AND '$lastDay'");
$stmt->execute();
$stmt->store_result();
$rows = $stmt->num_rows;
mysqli_stmt_bind_result($stmt, $date, $impressions, $balance, $username);
if ($rows > 0)
  {
    while (mysqli_stmt_fetch($stmt))
      {
        $ecpm_t3 = $balance * 1000;
        $ecpm3   = $ecpm_t3 / $impressions;
        $rate    = number_format((float)$ecpm3, 2, '.', '');
        echo "<tr>
                <td class=\"nn\" style=\"text-align:center;\" align=center>$date</td>
                <td class=\"nn\" style=\"text-align:center;\" align=center>$impressions</td>
                <td class=\"nn\" style=\"text-align:center;\" align=center>$$rate</td>
                <td class=\"nn\" style=\"text-align:center;\" align=center nowrap>$$balance</td>
              </tr>";
      }
   }
   else
     {
       echo "<tr>
               <td colspan=\"4\" class=\"nn\" style=\"text-align:center;\" align=center>Not rows</td>
             </tr>";
     }

回答by user1844933

Try this

尝试这个

<?php

$stmt = $mysqli->prepare("SELECT date, impressions, balance, username FROM reports WHERE username = '$username' and date between '$firstDay' AND '$lastDay'");
    $stmt->execute();
    $stmt->store_result();
    $rows = $stmt->num_rows;
    mysqli_stmt_bind_result($stmt,$date,$impressions,$balance,$username);
       if ($rows > 0){
       while (mysqli_stmt_fetch($stmt)) {   
           $ecpm_t3 = $balance*1000;
       $ecpm3 = $ecpm_t3/$impressions;
    $rate = number_format((float)$ecpm3, 2, '.', '');
     echo"<tr>
            <td class=\"nn\" style=\"text-align:center;\" align=center>$date</td>
            <td class=\"nn\" style=\"text-align:center;\" align=center>$impressions</td>
            <td class=\"nn\" style=\"text-align:center;\" align=center>$$rate</td>
            <td class=\"nn\" style=\"text-align:center;\" align=center nowrap>$$balance</td>
        </tr>";
       }} else {
        echo"<tr>
            <td colspan=\"4\" class=\"nn\" style=\"text-align:center;\" align=center>Not rows</td>
        </tr>";
    }


?>