我运行此查询以检查cat_name是否已经存在于mysql数据库中......但是它显示了警告消息。
警告: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();
}
}
}
?>
有什么想法吗?
答案 0 :(得分:2)
您的查询失败(并返回false
)。检查mysql_error
或mysql / php错误日志。
答案 1 :(得分:1)
来自文档:
返回SELECT, SHOW, DESCRIBE, EXPLAIN
和其他语句
结果集,mysql_query()
会在成功时返回资源,或FALSE
开启
错误。
mysql_query()
也将失败并返回FALSE
有权访问查询引用的表。
我最好的猜测是你的查询中有错误。