mysql_num_rows上的错误

时间:2011-06-28 10:59:03

标签: php mysql-num-rows

我的代码跟随iam收到错误“警告:mysql_num_rows():提供的参数不是有效的MySQL结果资源......”帮我解决这个问题

<?php
$type = $_REQUEST['type'];

#defauts
$maxRows_p = 10;
$pageNum_p = 0;
if (isset($_GET['pageNum_p'])) {
  $pageNum_p = $_GET['pageNum_p'];
}
$startRow_p = $pageNum_p * $maxRows_p;
$limit = ' LIMIT '.$startRow_p.', '.$maxRows_p;



//BUILD Addition Search Conditions
if(isset($_REQUEST['district']) && ($_REQUEST['district'] != 'All'))
    $search = ' district = '.$_REQUEST['district'];

if(isset($_REQUEST['city']) && ($_REQUEST['city'] != 'All'))
    $search = ' city = '.$_REQUEST['city'];


$search= ' availibility = "0" ';

$searchStr = @implode(' and ',$search);     

$sql = 'select * FROM properties WHERE type= "'.$type.'" and ';
$sql .= $searchStr;

## DEBUGi
//if($debugP) echo 'Zip Code Radius SQL<hr>'.$sql;

//Add column sorting

if($_REQUEST['sort'] != '')
    $sort = ' order by added asc ';
else
    $sort = $_REQUEST['sort'];

### DEBUG
if($debugP) echo 'Advanced Search Sql<hr>'.$sql;

$error['Results'] = 'Sorry no properties found.';



### Finished Building search sql and execting #####
$sql_with_limit = $sql . $sort . $limit;


if($debugP)
    echo "<hr>Property Search with Limit SQL: $sql_with_limit";     

//Perform search
$searchResults = mysql_query($sql.$sql_with_limit); 

### BUILD OUTPUT ####

if (isset($_GET['totalRows_p'])) {
  $totalRows_p = $_GET['totalRows_p'];
} else {
  if($debugP)
      echo "<hr>Property with out limit SQL: $sql $sort";
  $all_p = mysql_query($sql.$sort);
  $totalRows_p = mysql_num_rows($all_p); //$totalRows_p = mysql_num_rows($all_p);
  if($debugP)
      echo "<br>Result Rows $totalRows_p";
}
$totalPages_p = ceil($totalRows_p/$maxRows_p)-1;


if($debugP)
    echo "<hr>Builting Query String for Limit: ";

//Build query string
foreach($_GET as $name => $value){
    if($name != "pageNum_p")
        $queryString_p .= "&$name=$value";
}

if($debugP)
    echo $queryString_p;                    
?>

3 个答案:

答案 0 :(得分:0)

可能是因为MySQL无法按提供的方式处理您的SQL语句。 尝试将mysql_query行更改为以下内容,以便获得一些调试信息, 只需尝试下面的查询代码

 $all_p = mysql_query($sql.$sort) or die('Query failed: ' . mysql_error() . "<br />\n$sql");

感谢名单。

答案 1 :(得分:0)

我认为你的问题就在这里:

     $all_p = mysql_query($sql.$sort);
     $totalRows_p = mysql_num_rows($all_p);
从你得到的消息

$ all_p不是有效的资源。这可能是由于您传递的查询中的错误

你试过这个吗?

     $all_p = mysql_query($sql.$sort);
     if ($all_p !== false){
         $totalRows_p = mysql_num_rows($all_p);
     }

如果您没有看到任何警告您的查询有问题,那么您应该对其进行调试。尝试在mysql中执行查询并查看是否返回任何结果。

答案 2 :(得分:0)

当您想要数组分配时,您正在使用$search的直接分配。这意味着当你进行内爆时,它可能会返回false(???我从未在内爆时抑制警告。我不知道那是什么。)。附加到$ sql时被误读。然后将$ sort和$ limit附加到$ sql,然后在调用mysql_query之前再次预先添加$ sql。你也没有检查$ _REQUEST ['city']和$ _REQUEST ['desitination']是否有值。

你的最终结果(更糟糕的情况):

select * FROM properties WHERE type= "'.$type.'" and  select * FROM properties WHERE type= "'.$type.'" and order by added asc Limit 0,10

您的最终结果(最佳情况):

select * FROM properties WHERE type= "'.$type.'" and city = 'Atlanta' select * FROM properties WHERE type= "'.$type.'" and city = 'Atlanta' order by added asc Limit 0,10

在顶部添加:

$search = array();

然后您想要追加$search

$search[] = ' district = '.$_REQUEST['district'];

并将此行$searchResults = mysql_query($sql.$sql_with_limit);替换为:

$searchResults = mysql_query($sql_with_limit); 

顺便说一句,你在这里请求SQL注入。只要您从$_REQUEST中提取值,就应该使用mysql_real_escape_string