【问题标题】:mysql fetch array if no results display message如果没有结果显示消息,mysql fetch array
【发布时间】:2011-06-01 19:21:15
【问题描述】:

我正在尝试进行数据库调用以显示一条语句,如果没有返回结果,则说明没有找到结果。

我将如何对我的代码执行此操作:-

$getFixtures = mysql_query("SELECT ht.name AS hometeam_name, homescore, awayscore, at.name AS awayteam_name, time, date, week, comp.competition AS comp_name, se.name AS season_name
                    FROM fixture
                    JOIN team ht
                    ON ht.id = fixture.hometeam
                    JOIN team at
                    ON at.id = fixture.awayteam
                    JOIN competition comp
                    ON comp.id = fixture.competition
                    JOIN season se
                    ON se.id = fixture.season
                    WHERE se.name = '$season' AND comp.competition = '$competitiontitle' AND date >= '$today' AND at.name = '$teamName' OR ht.name = '$teamName' AND se.name = '$season' AND comp.competition = '$competitiontitle' AND date >= '$today'
                    ORDER BY date ASC
                    ");                     
                    while ($fixtureData = mysql_fetch_array($getFixtures))
                    {
                    $hfixteamlink = strtolower(str_replace(" ","-",$fixtureData['hometeam_name']));
                    $afixteamlink = strtolower(str_replace(" ","-",$fixtureData['awayteam_name']));
                    $date = date("d/m/Y", strtotime($fixtureData['date']));
                    ?> 

提前致谢

理查德

【问题讨论】:

  • 这闻起来像是 if 声明的好候选,如果我见过的话!

标签: php mysql arrays fetch


【解决方案1】:

这需要一个 IF 语句。

$rows = mysql_fetch_array($getFixtures);
if(count($rows))
{
    while ($fixtureData = $rows)
    ...
}
else
{
    echo 'No results found';
}

【讨论】:

    【解决方案2】:
    if ( ! mysql_num_rows($getFixtures)) {
       echo 'No results found.';
    } else {
      ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-28
      • 2016-12-19
      • 2015-12-15
      相关资源
      最近更新 更多