简体   繁体   English

MySQL Num行问题

[英]Mysql num rows issue

I run this query to check if cat_name already exists it the mysql database... but it's show a warning messag. 我运行此查询以检查cat_name在mysql数据库中是否已经存在...但是它显示警告消息。

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\\xampp\\htdocs......" 警告:mysql_num_rows()期望参数1为资源,在C:\\ xampp \\ htdocs中给定布尔值……”

<?php
include("db.php");

if(isset($_POST['cat_name']))
{
$cat_name = mysql_real_escape_string(htmlentities(trim($_POST['cat_name'])));   
$err = array();

$ch = mysql_query("SELECT cat_name FROM categories WHERE cat_name = '$cat_name' ");
$num = mysql_num_rows($ch);

if(empty($cat_name))
$err[] = "Category field empty";
    elseif(is_numeric($cat_name))
    $err[] = "Category name should be string, ex: category name";
        elseif($num > 0)
        $err[] = "Category name already exits, please choose another name";
            else
            {
                if(strlen($cat_name) < 3)
                $err[] = "Category name at least 3 or more    
characters";    
            }

if(!empty($err))
{
    foreach($err as $er)
    {
        echo "<font color=red>$er.</font>";
    }   
}
else
{
    $sql_insert = mysql_query("INSERT INTO categoires VALUES('',  
'$cat_name')"); 

    if($sql_insert)
    {
        echo "Successfully inserted your category name";    
    }
    else
    {
        echo "Something is wrong to add your cateogory name";
        mysql_error();  
    }

}


}
?>

Any idea? 任何想法?

Your query is failing (and returning false ). 您的查询失败(并返回false )。 Check mysql_error or the mysql/php error logs. 检查mysql_error或mysql / php错误日志。

From the docs: 从文档:

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error. 对于SELECT, SHOW, DESCRIBE, EXPLAIN和其他返回结果集的语句, mysql_query()成功时返回资源,错误时返回FALSE

mysql_query() will also fail and return FALSE if the user does not have permission to access the table(s) referenced by the query. 如果用户无权访问查询所引用的表, mysql_query()也将失败并返回FALSE

My best guess is that you have an error in your query. 我最好的猜测是您的查询中有错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM