简体   繁体   English

mysql_num_rows 上的错误

[英]error on mysql_num_rows

my code is following iam getting a error on "Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource..." help me to solve this我的代码跟随 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;                    
?>

It probably because that MySQL was unable to process your SQL statement as supplied.这可能是因为 MySQL 无法处理您提供的 SQL 语句。 Try changing your mysql_query line to the following so that you get some debug information, just try below code for your query尝试将您的 mysql_query 行更改为以下内容,以便获得一些调试信息,只需尝试以下代码进行查询

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

Thanx.谢谢。

I think that your problem is here:我认为你的问题在这里:

     $all_p = mysql_query($sql.$sort);
     $totalRows_p = mysql_num_rows($all_p);

from the message you get $all_p is not a valid resource.从您收到的消息中 $all_p 不是有效资源。 This might be due to an error in the query you are passing这可能是由于您传递的查询中的错误

have you tried doing this?你试过这样做吗?

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

if you see no warning you have a problem with your query and you should debug that.如果您没有看到任何警告,则说明您的查询有问题,您应该对其进行调试。 Try to execute you query in mysql and look if any results are returned.尝试在 mysql 中执行查询并查看是否返回任何结果。

You're using direct assignment for $search when you want array assignment.当您需要数组分配时,您正在对$search使用直接分配。 This means that when you go to implode it, it probably returns false(??? I've never suppressed warnings on implode. I have no idea what that does.).这意味着当你 go 内爆它时,它可能会返回 false(???我从来没有抑制内爆警告。我不知道那是什么。)。 That is being misread when appended to $sql.附加到 $sql 时会被误读。 You then append $sort and $limit to $sql and then prepend $sql again for good measure before calling mysql_query.然后,您将 append $sort 和 $limit 设置为 $sql,然后在调用 mysql_query 之前再次添加 $sql 以获得良好的度量。 You're also not checking to see if $_REQUEST['city'] and $_REQUEST['desitination'] have values.您也没有检查 $_REQUEST['city'] 和 $_REQUEST['destination'] 是否有值。

Your final result (worse case):您的最终结果(更糟糕的情况):

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

Your final result (best case):您的最终结果(最佳情况):

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

Add this at the top:在顶部添加:

$search = array();

and then any time you want to append $search :然后任何时候你想 append $search

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

And replace this line $searchResults = mysql_query($sql.$sql_with_limit);并替换这一行$searchResults = mysql_query($sql.$sql_with_limit); with:和:

$searchResults = mysql_query($sql_with_limit); 

BTW you're begging for SQL injection here.顺便说一句,您在这里乞求 SQL 注射。 Any time you pull a value from $_REQUEST , you should be using mysql_real_escape_string .任何时候你从$_REQUEST中提取一个值,你都应该使用mysql_real_escape_string

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

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