php - 警告 : mysql_num_rows() expects parameter 1 to be resource, boolean 值

标签 php mysql boolean warnings

这个问题在这里已经有了答案:





Reference - What does this error mean in PHP?

(37 个回答)



ERROR: Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\...\....php on line 19 [duplicate]

(7 个回答)


8年前关闭。




这是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 是资源, boolean 值在第 69 行的 C:\Users\Jared\Desktop\xampp\htdocs\tournaments\NewTournament.inc.php 中给出

最佳答案

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

我将在任何查询中添加错误检查作为个人实践,以防发生类似情况。它可以像这样简单:

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

附带说明一下,php 中的 mysql 函数已被贬值。您可能要考虑改用 Mysqli 或 PDO。

关于php - 警告 : mysql_num_rows() expects parameter 1 to be resource, boolean 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18578732/

相关文章:

java - 在简单的java程序中使用IF语句来应用折扣

php - 循环从按关系排序和摸索的整个表中选择一个不同的行

PHP 运费计算器

javascript - 编辑内容可编辑的页面并使用 php 保存

php - SQL : Select last 5 unique results from database?

MySQL - FIND_IN_SET 、 GROUP_CONCAT 问题

mysql - 将mysql列拆分为数字和字符

c#-4.0 - 在运行时创建 Func<T,Bool>

Mysql复杂的select语句

python - 这里 "if False:"的作用是什么?