Php sql查询有时会返回null

时间:2013-10-21 12:01:14

标签: php mysql

在我的PHP中,我对我的数据库进行了一个sql查询,这是一个团队列表。在某些团队中,查询返回null。如果我在phpmyadmin中进行相同的查询,则返回我寻找的值。

我的查询代码:

$sql = $mysqli->query("
    SELECT `team`
    FROM `dota teams`
    WHERE `team` LIKE '%$team%'
    OR `teamalt` LIKE '%$team%'
")  or die($mysqli->error);

例如,如果来自我网站的查询是te,则返回null,但如果查询是titan,则返回Titan Esports。使用Virtus Pro,如果我使用vp或virtus,它会返回我想要的值。使用NAVI我返回null无论是什么。如果我写EG或邪恶,邪恶的天才也会返回价值。

为什么用某个名字可以得到一个返回的值而有些没有?

enter image description here

完整的PHP代码:

$date = $mysqli->real_escape_string(date("d\-m\-Y"));

$team = set_space($team); //Set spaces in teams so we can ute it for search

// Get team name
$sql = $mysqli->query("
    SELECT `team`
    FROM `dota teams`
    WHERE `team` LIKE '%$team%'
    OR `teamalt` LIKE '%$team%'
")  or die($mysqli->error);

if($sql->num_rows>0){
    $team = $sql->fetch_array();
    $team = $team['team'];

// Get all the matches
    $sql = $mysqli->query("
        SELECT * FROM `dota schedule`
        WHERE `date` >= '$date' AND `teams`
        LIKE '%$team%' ORDER BY `date`,`time` ASC
    ")  or die($mysqli->error);

    // Loop through all the results
    while ($data = $sql->fetch_array()){
        // Change the time based on timezone
        $time_arr = str_split($data['time'],3);
        $hour = $time_arr[0] + $time_add;
        $min = str_replace(":","",$time_arr[1]);
        $time = "$hour : $min";

        // Get teams
        $teams = get_teams($data['teams']);

        // Get casters
        $caster = explode('_', $data['caster']);
        foreach($caster as &$c){
            $c = get_string_between($c,'[',']');
        }
        // Loop through to see which language the casters has.
        foreach($caster as &$c){
            $sqls = $mysqli->query("SELECT `language` , `stream` FROM `dota casters` WHERE `name` = '$c'") or die($mysqli->error);
            $da = $sqls->fetch_array();
            if($da['language'] == "English"){
                $stream = $da['stream'];
                $c = "<a href='$stream'><img src='http://joffe.kottnet.eu/flags/uk.png' alt='English'>$c</a>";
            }
            else if($da['language'] == "Russia"){
                $stream = $da['stream'];
                $c = "<a href='$stream'><img src='http://joffe.kottnet.eu/flags/russia.png' alt='English'>$c</a>";
            }
        }
    ?>
        // Write out table.
        <tr>
            <td><?= "Date:" . $data['date'] . " Time: " .$time?></td>
            <td><?= $data['cup'] ?></td>
            <!-- 3 TD for teams -->
            <td class="team1"><?= $teams[0] ?></td>
            <td class="vs">VS</td>
            <td><?= $teams[1] ?></td>
            <td><?= print_out_array($caster); ?></td>
        </tr>
    <?php
    }
}

2 个答案:

答案 0 :(得分:0)

对于这个问题,“为什么用某个名字可以得到一个返回的值而有些没有?”,这是因为你的查询只选择一个字段,团队,但你的where子句正在查看两个字段,用或逻辑。

因此,如果您有一个记录,其中团队字段为空且茶色域字段的值为'te',那么以'te'作为搜索参数的查询将返回一行具有空值。

该怎么做取决于您的要求。怎么做取决于你的数据库引擎似乎没有指定。

答案 1 :(得分:0)

也许你的问题在

$team = set_space($team);

我不确定,因为你没有从$team来自哪里发帖。您可以通过使PHP代码echo查询来检查您的查询是否写得很好。在回声之后评论所有内容,这样你就不会收到任何错误。

如果查询错误,请尝试将其更改为:

$sql = $mysqli->query("
    SELECT `team` 
    FROM `dota teams` 
    WHERE `team` LIKE '%" . $team . "%' 
    OR `teamalt` LIKE '%" . $team . "%'
")  or die($mysqli->error);.