[英]error on 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;
?>
這可能是因為 MySQL 無法處理您提供的 SQL 語句。 嘗試將您的 mysql_query 行更改為以下內容,以便獲得一些調試信息,只需嘗試以下代碼進行查詢
$all_p = mysql_query($sql.$sort) or die('Query failed: ' . mysql_error() . "<br />\n$sql");
謝謝。
我認為你的問題在這里:
$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 中執行查詢並查看是否返回任何結果。
當您需要數組分配時,您正在對$search
使用直接分配。 這意味着當你 go 內爆它時,它可能會返回 false(???我從來沒有抑制內爆警告。我不知道那是什么。)。 附加到 $sql 時會被誤讀。 然后,您將 append $sort 和 $limit 設置為 $sql,然后在調用 mysql_query 之前再次添加 $sql 以獲得良好的度量。 您也沒有檢查 $_REQUEST['city'] 和 $_REQUEST['destination'] 是否有值。
您的最終結果(更糟糕的情況):
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();
然后任何時候你想 append $search
:
$search[] = ' district = '.$_REQUEST['district'];
並替換這一行$searchResults = mysql_query($sql.$sql_with_limit);
和:
$searchResults = mysql_query($sql_with_limit);
順便說一句,您在這里乞求 SQL 注射。 任何時候你從$_REQUEST
中提取一個值,你都應該使用mysql_real_escape_string
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.