警告:mysql_num_rows()期望参数1是资源,布尔值

时间:2013-09-02 18:08:53

标签: php mysql boolean warnings

这是php

<?php
if (
isset($_POST['tour_name'])&&
isset($_POST['pot'])&&
isset($_POST['max_players'])&&
isset($_POST['min_players'])){

    $tour_name = $_POST['tour_name'];
    $pot = $_POST['pot'];
    $max_players = $_POST['max_players'];
    $min_players = $_POST['min_players'];

    if (!empty($tour_name)&&!empty($pot)&&!empty($max_players)&&!empty($min_players)) {
        if (($min_players >= 2) && ($min_players <= 6)) {
            if (($max_players >= 2) && ($min_players <= 12)) {
                if (($pot >= 1) && ($pot <= 100)) {

                    $query = "SELECT `tour_name` FROM `tournies` WHERE `tour_name`='$tour_name'";
                    $query_run = mysql_query($query);

                    if (mysql_num_rows($query_run)==1) {
                    echo 'There is already a tournament with the name '.$tour_name.'.';
                    } else {
                        $query = "INSERT INTO `tournies` VALUES ('', '".mysql_real_escape_string($tour_name)."', '".mysql_real_escape_string($pot)."', '".mysql_real_escape_string($max_players)."', '".mysql_real_escape_string($min_players)."')";
                        if ($query_run = mysql_query($query)) {
                            header('Location: table.php');
                        } else {
                            echo 'Registration was not a win.';
                        }
                    }

                } else {
                    echo 'The pot must be 1-100';
                }
            } else {
                echo 'Maximum players must be atleast 2 and no more then 12';
            }
        } else {
            echo 'Minimum players must be atleast 2, and no more then 6';
        }
    } else {
        echo 'All fields are required';
    }
}

?>

错误是 警告:mysql_num_rows()期望参数1为资源,布尔值在第69行的C:\ Users \ Jared \ Desktop \ xampp \ htdocs \ tournaments \ NewTournament.inc.php中给出

2 个答案:

答案 0 :(得分:1)

您的查询可能失败了。使用mysql_error()确定错误。

我将个人做法添加到任何查询中都是错误检查,以防万一这样的事情发生。它可以这么简单:

if(!$query_run){
    echo "Error in query:" . $query . "   MYSQL Error:" . mysql_error();
}

作为旁注,php中的mysql函数已被折旧。您可能需要考虑使用Mysqli或PDO。

答案 1 :(得分:1)

你可以尝试:

if ($query_run !== FALSE && mysql_num_rows($query_run)==1) {

看起来您的查询运行可能有问题,当您通过phpMyAdmin或通过控制台手动运行查询时会得到什么?