PHP - While/Else 错误?

PHP - While / Else error?(PHP - While/Else 错误?)
本文介绍了PHP - While/Else 错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有以下 php 代码:

I have the following php code:

<?php




if (!isset($_REQUEST['search'])){

    while(($write=mysql_fetch_array($gamesearched)) != null){
    echo "Found!";
    }else{
    echo "No results";
    }

    }
?>

它给了我一个错误:

解析错误:语法错误,意外的else"(T_ELSE) inC:phpwwwGameplayackgame.php 第 41 行

Parse error: syntax error, unexpected 'else' (T_ELSE) in C:phpwwwGameplayackgame.php on line 41

推荐答案

在 PHP 中,while 语句不能有 else 子句.你需要在 while 之外的东西,它可以告诉你它是否至少被执行过一次.

In PHP, a while statement can't have an else clause. You need something external to the while that can tell you if it was executed at least once.

这样的事情怎么样?

$total = mysql_num_rows($gamesearched);
if ($total > 0) {
    while (($write=mysql_fetch_array($gamesearched)) !== false) {
        echo "Found!";
    }
} else {
    echo "No results";
}

在这种情况下,我在开始之前查找了找到的总行数,但我也可以通过将计数器设置为零然后在 while 循环内递增它来开始.看起来像这样:

In this case, I've looked up the total number of rows found before I start, but I could also have started by setting a counter to zero and then incrementing it inside the while loop. That would look something like this:

$total = 0;
while (($write=mysql_fetch_array($gamesearched)) !== false) {
    $total++;
    echo "Found!";
}
if ($total == 0) {
    echo "No results";
}

请注意,如果没有更多行,mysql_fetch_array() 将返回 false,因此我也为您更新了 while 条件.

Note that mysql_fetch_array() returns false if there are no more rows, so I've updated the while condition for you as well.

综上所述,有充分的理由不在新代码中使用 mysql_* 函数.有关更多详细信息和一些更好的选择,请参阅此问题:为什么不应该我在 PHP 中使用 mysql_* 函数?

All that being said, there are good reasons not to use mysql_* functions in new code. See this question for more details, and some better alternatives: Why shouldn't I use mysql_* functions in PHP?

这篇关于PHP - While/Else 错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)