开发者

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

<?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. Try changing your mysql_query line to the following so that you get some debug information, just try below code for your 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. 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.


You're using direct assignment for $search when you want array assignment. 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.). That is being misread when appended to $sql. You then append $sort and $limit to $sql and then prepend $sql again for good measure before calling mysql_query. You're also not checking to see if $_REQUEST['city'] and $_REQUEST['desitination'] have values.

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:

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

And replace this line $searchResults = mysql_query($sql.$sql_with_limit); with:

$searchResults = mysql_query($sql_with_limit); 

BTW you're begging for SQL injection here. Any time you pull a value from $_REQUEST, you should be using mysql_real_escape_string.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜