MySQL / phpMyAdmin查询行计数(1行)vs mysql_num_rows'计数(0行计数)

时间:2012-03-29 18:23:31

标签: php mysql sql

以下是相关代码:

<?php // If search option is selected
 include 'db_connect.php';

 database_connect();
 $limit = 5;  // No of result per page
 $criteria = 1; // Search criteria
 $allResults=array();  // Array of results
 $searchOption = $_REQUEST['searchoption'];
 $searchField = $_REQUEST['searchfield'];
 $searchField = str_replace('+',' ', $searchField) ;
 $dbcolumn = "user_id";
 $dbtable = "profiles";
 $order = "user_id";

 if ($searchField){
    if ($searchOption == "People"){
    $words = explode(' ', trim($searchField));
    $searchField = $words[0];
    $dbcolumn = "profile_display_name";
    $dbtable = "profiles P, user_roles U, roles R";
    $order = "profile_first_name";
    $criteria =     " P.user_id = U.user_id AND U.role_id = R.role_id 
                    AND(`profile_display_name` LIKE '%$searchField%'   
                    OR `profile_first_name` LIKE '%$searchField%'   
                    OR `profile_last_name` LIKE '%$searchField%' 
                    OR `profile_email` LIKE '%$searchField%' 
                    OR `profile_state` LIKE '%$searchField%' 
                    OR `profile_zip_code` LIKE '%$searchField%'
                    OR `business_summary` LIKE '%$searchField%')";
    }
    //focus here folks (my comment to readers, not actual part of code
    else if ($searchOption == "Jobs"){
    $dbcolumn = "job_title";
    $dbtable = "job_postings";
    $order = "`created_date` DESC";
    $criteria = "(`job_title` LIKE '%$searchField%'   
    OR `job_description` LIKE '%$searchField%'   
    OR `client_name` LIKE '%$searchField%' 
    OR `city` LIKE '%$searchField%' 
    OR `state_abbr` LIKE '%$searchField%' 
    OR `zipcode` LIKE '%$searchField%') 
    AND `job_status` = 'Open'";
    }
    else if($searchOption == "News"){
    $dbcolumn = "";
    $dbtable = "";
    $order = "";
    $criteria = "1";   
    }

  //Pagination  
 if($dbtable){
    $rs = mysql_query("SELECT * FROM $dbtable WHERE $criteria");
    $no_of_rec =  mysql_num_rows($rs);
    $page = ($_REQUEST['page'])? $_REQUEST['page'] : 1;
    $no_of_pages = ceil($no_of_rec/$limit);
    $offset  = ($page - 1)*$limit;

    echo "pagination #rows: $no_of_rec ";
 } 
}

// If the user enters a value in the search
if ($searchField){ 
   $resultcount = 0;
   if($dbtable)   {
    //  Search query executes
    $query = "SELECT * FROM $dbtable WHERE $criteria ";
    $query .= "ORDER BY $order ";
    $query .= "LIMIT $offset, $limit";
    $result = mysql_query($query) or die(mysql_error());  
    $resultcount = mysql_numrows($result);  

    echo "query: $query ";
    echo "result: $result ";
    echo "result count: $resultcount ";     
 }

 if ($resultcount <= 0){
     $allResults[] = "No match found";
 }
 else{
// Get search results from database
    while ($row = mysql_fetch_array($result)){
$allResults[]=$row;
  }

 }
}
    else{
     $allResults[] = "No value entered";
    }
  ?>

以下是echo语句显示的内容: pagination #rows: 0 query: SELECT * FROM job_postings WHERE (`job_title` LIKE '%Help Wanted%' OR `job_description` LIKE '%Help Wanted%' OR `client_name` LIKE '%Help Wanted%' OR `city` LIKE '%Help Wanted%' OR `state_abbr` LIKE '%Help Wanted%' OR `zipcode` LIKE '%Help Wanted%') AND `job_status` = 'Open' ORDER BY `created_date` DESC LIMIT 0, 5 result: Resource id #36 result count: 0

(我留下了回音打印输出,因此你可以看到我所看到的以及我是如何看到的。)

当我在MySQL和phpMyAdmin中运行查询时,我得到了我正在寻找的行。

有人可以解释为什么mysql_numrows()认为有0个结果吗?

我已经检查了PHP.net,我也用Google搜索了这个。到目前为止没有运气。

非常感谢。

1 个答案:

答案 0 :(得分:1)

此行中存在错误类型

 $resultcount = mysql_numrows($result); 

你应该调用mysql_num_rows()

您可以选择使用此代码

$isExist = mysql_query("Select count(id) from ..."); 
$r = mysql_fetch_array($isExist);
if($r['COUNT(id)'] > 0){
//item exists
}else{
//item doesnt exist
}